# Offboard a leaver

> Stop a departing user's sign-in and tokens at once, end the sessions they hold, and delete their account when you no longer need it.

Ben leaves today. By the end of this guide he can't sign in, no app can get a token for him, every session he had
open is over, and his groups no longer list him. His record stays, inactive, for as long as you need it, and you can
delete it later in one call.

**Before you start:** you're a realm administrator holding `RealmOwner`, with your session in `$CVYUH_SESSION` (see
[Call the API](../../../develop/api/)). You need the user's id and their sign-in name (`uid`), here both `ben`.

<Aside type="note" title="One search, two places">
  Lists and searches take a search document: `{ "filter": { "field": …, "op": …, "value": … } }`. On a `GET` you send
  it as the `filter` query parameter. In the body of a sessions `end` call you send the same document as `qbe`. See
  [Search and paging](../../users/search/).
</Aside>

## Offboard the user

<Steps>

1. Set him inactive:

   ```sh
   curl -X PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/user/ben \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "status": "inactive" }'
   ```

   From now on his sign-in is refused, no app gets a new token for him, his refresh tokens stop working, and his
   tokens read as inactive to any app that asks the realm. An access token he already holds still works, at an app
   that checks it itself, until it expires (five minutes by default).

2. End the sessions he still holds, found by his sign-in name:

   ```sh
   curl https://foobar.cvyuh.com/api/v0/am/sessions/v1/end \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "qbe": { "filter": { "field": "user", "op": "=", "value": "ben" } } }'
   ```

   The realm answers `{ "ended": <n> }`. Each session ends as a sign-out does, and the apps it signed in to are told.

3. Remove him from every group at once, by setting his groups to an empty list:

   ```sh
   curl -X PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/user/ben \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "memberOf": [] }'
   ```

   Each of his groups stops listing him in the same write. To remove him from one group only, use `DELETE` on
   `/objects/v1/group/{group}/relationships/member/ben` with `If-Match: *`.

4. Hand over his agents. Agents acting for him already get nothing for him, but agents he owns go on, and would be
   left with no owner if you deleted him. List them:

   ```sh
   curl "https://foobar.cvyuh.com/api/v0/am/objects/v1/user/ben?expand=ownedAgents/*" \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION"
   ```

   For each one, remove Ben as its owner and name the new one, here Maya:

   ```sh
   curl -X DELETE https://foobar.cvyuh.com/api/v0/am/objects/v1/agent/invoice-agent/relationships/businessOwner/ben \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "If-Match: *"

   curl https://foobar.cvyuh.com/api/v0/am/objects/v1/agent/invoice-agent/relationships/businessOwner \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -H "If-Match: *" \
     -d '{ "targetId": "maya" }'
   ```

   Or stop an agent no one needs: see [Stop an agent](../../../workforce-identity/guides/agents/stop-an-agent/).

</Steps>

<Aside type="tip" title="Secrets shared with him">
  If your realm uses the secrets store, take back every secret anyone shared with him, in every vault, in one call,
  then change the secrets he could read. See [Offboard someone](../../../secrets-store/guides/offboard-someone/).
</Aside>

## Check it worked

- His live sessions are gone:

  ```sh
  curl -G https://foobar.cvyuh.com/api/v0/am/sessions/v1 \
    -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
    --data-urlencode 'filter={"filter":{"field":"user","op":"=","value":"ben"}}'
  ```

- A read of `/objects/v1/user/ben?expand=memberOf/*` shows `status` `inactive` and no groups, and one with
  `?expand=ownedAgents/*` shows no agents.
- A sign-in as `ben` is refused.

## Delete the account

When you no longer need his record, delete it:

```sh
curl -X DELETE https://foobar.cvyuh.com/api/v0/am/objects/v1/user/ben \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
  -H "If-Match: *"
```

This also removes every link to him and his personal vault, with every secret in it. It can't be undone. Any agent
still naming him as owner is left with none, which is why step 4 comes first.

## Undo it

Before you delete the account, everything is reversible. Set `{ "status": "active" }` and add him back to his groups,
and he can sign in again with what he had. Note his groups before step 3 if he may come back.

## Related

- [Joiners, movers and leavers](../../lifecycle/joiners-movers-leavers/): every step of a user's time in the realm.
- [Turn access off and on](../../users/turn-access-off-and-on/): what `status` and `accountExpiry` stop.
- [Deleting an owner](../../../secrets-store/vaults/owner-deletion/): what deleting a user does to their secrets.
