# Removing or moving a host

> Take a host out of the market, remove it, uninstall the agent, or move it to another organization.

Source: https://legion.ambient.xyz/docs/providing/removing-a-host

## Remove a host

1. On the host's page, press **Stop new rentals**. Rentals already running continue.
2. Wait until no rentals are active on it. Until then, removing it fails with `409 host_has_active_rentals`.
3. Press **Remove host** and confirm.

The host leaves the market and your host list, and its agent's certificate is revoked. **Show removed** in **Hosts** brings it back into view. To add the machine again, create a new install command.

## Renters' files on the host

A stopped or failed rental keeps its files on the host for up to 72 hours, so its renter can start it again or take a backup. A host honours the files it holds: while any are left, **Remove host** shows them (rental, state, size and when they are deleted) and asks you to choose. Both choices are always shown:

- **Remove when empty** (selected by default). The host leaves offers at once and its rentals can't start. Each renter is emailed that the host is leaving, with their delete-by time and a link to back their files up, which is their choice and at their cost. The host shows **Removing when empty** and is removed on its own once no rental has files left, within 72 hours at the latest. Files left at that point are deleted.
- **Delete renters' files and remove now**. You type the host's name and say why. The host's agent deletes the files and confirms, and the host shows **Removing: deleting renters' files** until then, for at most an hour. Each renter is emailed that their files were deleted because the host was removed (`data_deleted_reason` `host_removed`). The removal and your reason are recorded against your host and organization, and Legion staff can see them.

While a host is **Removing when empty** you can still press **Remove host** to delete the files now. While it is deleting them there is nothing left to choose. A host that is already removing can't be listed again (`409 host_removing`).

A host with no files on it is removed at once, as before. Through the API, a removal that meets files is refused with `409 host_has_stored_data` until the request names `stored_data`; `getHostRemovalPreview` lists the files first.

## Uninstall the agent

Delete the host's rentals first. Then, on the machine:

```sh
sudo systemctl disable --now legion-agent
sudo rm -rf /usr/local/bin/legion-agent \
  /etc/systemd/system/legion-agent.service \
  /etc/systemd/system/legion-agent.service.d \
  /etc/legion-agent \
  /var/lib/legion-agent
```

Removal doesn't touch rental containers or their networks.

## Move a host to another organization

1. Delete the host's rentals, and wait until they show **Deleted**.
2. Stop the agent: `sudo systemctl stop legion-agent`.
3. Remove its identity and configuration: `sudo rm -rf /var/lib/legion-agent /etc/legion-agent`.
4. In the new organization, create an install command and run it.

The machine joins as a new host. The old host stays offline in the old organization.
