# Device removal

> Remove a device for good; every part of the realm forgets it before its record goes.

Remove a device when it's retired, wiped or gone for good. The realm doesn't just delete a row: it cuts the device's
connection, deletes its security sensor's registration from the security log, drops it from the AI fleet, and only
then removes its record. Nothing is left half-known.

## Before you begin

- **Privilege:** removing needs `MODIFY` on the realm's endpoint settings, which `RealmOwner` holds
  ([Who can call](../../api/#who-can-call)).
- **Where:** the API.
- **The device id**, the `id` on its [Device records](../../devices/device-records/) entry.

## How it works

1. **You remove it.** The record turns `removing`, and from that moment the device is refused as a revoked one is.
2. **Each part forgets it.** The device's open streams are cut, its sensor's registration and its system inventory
   are deleted, and the AI fleet drops the device.
3. **The record goes**, once each part has forgotten it. Its place under the realm's
   [Device limits](../../enrollment/device-limits/) is free again.

If removal is interrupted, the realm finishes it on its own; the device stays `removing` until then.

## Set it up

Remove the device by its device id ([Call the API](../../../develop/api/)):

```sh
curl -X DELETE https://foobar.cvyuh.com/api/v0/endpoint/ppm/v1/<device id> \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION"
```

The realm answers `202`: the removal is accepted and finishes in the background. An optional `If-Match` with the
record's `ETag` refuses a removal made over someone else's change.

To tell when it's done, read the record: it's `removing` while it runs, and answers `404` once it's gone.

```sh
curl https://foobar.cvyuh.com/api/v0/endpoint/ppm/v1/<device id> \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION"
```

On the device itself, uninstalling the package stops the device client and removes it with the sensor and the AI
tools agent.

## What the user sees

Nothing on the screen. The laptop keeps working and its data stays; the device client is refused from the moment
of removal and connects to nothing again. Removing doesn't wipe the device: uninstall the package there too.

## If it fails

| Status | When | What to do |
|---|---|---|
| `403` | You lack `MODIFY` on the realm's endpoint settings | Ask a realm administrator to [grant it](../../api/#who-can-call) |
| `404` | No device with that id in this realm, or it's already gone | Nothing to do if you removed it before |
| `412` | The record changed since the `ETag` you sent | Read the record again and send its new `ETag` |

## Limits and defaults

| What | Value |
|---|---|
| Answer | `202`, at once; the removal finishes in the background |
| Undo | None |
| Devices per call | One |

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| The record stays `removing` | A part of the realm hasn't confirmed yet | Wait: the realm keeps trying until it's done. Nothing else is needed |
| The device cap still counts the device | Its removal hasn't finished | Wait until its record answers `404` |
| The device's sensor is still in the Detection & Response sensors list | Its removal hasn't finished | Wait until its record answers `404` |

## Audit record

The start of a removal is recorded on the realm's audit trail as a change to the device's record (`PATCHED`, its
`status` turning `removing`), with who made it, and the record going as `DELETED`. See
[What is recorded](../../../audit-reports/audit-trail/what-is-recorded/).

## Good to know

- **There's no undo.** A removed device comes back only by enrolling again
  ([Device enrollment](../../enrollment/device-enrollment/)) with a new token.
- **Removing twice is harmless.** A device already being removed is left as it is.
- **Uninstalling isn't removing.** Uninstalling the package leaves the device's record in the realm; remove it there
  too.
- **No automatic cleanup.** Devices that stop reporting aren't removed on their own; remove them yourself.

## Related

- [Cut off a lost device](../../guides/cut-off-a-lost-device/): revoke first, then remove.
- [Device revocation](../revocation/): cut a device off without forgetting it.
- [Enroll a device again](../../guides/enroll-a-device-again/): remove the old record once the device is back under
  its new one.
