# End a user's sessions

> Sign a user out of the realm everywhere at once, when a device is lost or an account looks wrong, and see what it ends.

A user reports a stolen laptop, or their account is signing in from somewhere it shouldn't. Ending their sessions
signs them out of the realm everywhere at once: every app they signed in to through those sessions is told, and the
tokens issued under them stop working. The user signs in again as normal, so pair this with a password change or
removing a lost factor when the account itself is at risk.

**Before you start:**

- **Who can do it:** a realm administrator, in a group that holds `RealmOwner`
  ([Delegated administration](/platform/realms/delegated-administration/)). `RealmViewer` can read the settings but
  not change them.
- **Where:** today this is done through the API; [Call the API](/develop/api/) shows how to get `$CVYUH_SESSION`.
- **First:** have the user's ID: the `id` the realm gave them, read from
  `https://foobar.cvyuh.com/api/v0/am/objects/v1/user/maya`.

This guide ends sessions only. To cut a user off entirely, with their offline app access and their agents, follow
[Cut a user off now](/workforce-identity/guides/sessions/cut-a-user-off-now/).

## End the sessions

<Steps>

1. List the user's live sessions, to see where they're signed in:

   ```sh
   curl 'https://foobar.cvyuh.com/api/v0/am/sessions/v1?filter=<filter>' \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION"
   ```

   where `<filter>` is this, URL-encoded:

   ```json
   {"filter":{"field":"user","op":"=","value":"<user id>"}}
   ```

   Each session shows its `id`, when it began and was last used, and the address it signed in from.

2. End all of them:

   ```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": "<user id>" } } }'
   ```

   The realm answers with how many it ended, such as `{ "ended": 2 }`. To end only one, send
   `DELETE https://foobar.cvyuh.com/api/v0/am/sessions/v1/<id>` instead.

</Steps>

## Check it worked

- List the user's sessions again with the same `GET`: none is listed.
- Any app they were signed in to has received a back-channel logout, and its tokens from those sessions read inactive
  at introspection.
- The user's trail holds one `LOGOUT` record for each session ended, with your ID as `endedBy` in its context. Read it
  in **Audit → Authentication** in the dashboard, or with
  [Read a user's or agent's trail](/audit-reports/audit-trail/read-a-trail/).

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| `{ "ended": 0 }` | The filter's `value` isn't the user's ID (a username matches nothing), or they have no live session | Read the user's `id` and list their sessions first |
| `404` when ending one session | That session already ended, or its name is wrong | List the sessions again; the `id` column is the name to send |
| The user is still using an app | The app holds offline access, which outlives sessions, or keeps its own session and has no back-channel address | Revoke the app's grant ([Cut a user off now](/workforce-identity/guides/sessions/cut-a-user-off-now/)), and see [Tell other apps about a sign-out](../tell-apps-about-sign-out/) |
| Some sessions remain after a large search | One call ends at most 10,000 sessions | Call again |
| The user signs straight back in | Ending a session doesn't stop them signing in | Deactivate the user, or reset the lost factor |

## Good to know

- **Offline access isn't a session.** An app the user allowed `offline_access` keeps refreshing after their sessions
  end. See [Choose online or offline access](../online-and-offline-access/).
- **The user isn't told.** Their next click in an app sends them to sign in.

## Undo it

A session can't be brought back. The user signs in again and gets a new one.

## Related

- [Cut a user off now](/workforce-identity/guides/sessions/cut-a-user-off-now/): sessions, apps, agents and the
  account, in one runbook.
- [Session management](../../../authentication/session-management/): every way to find and end sessions.
- [Tell other apps about a sign-out](../tell-apps-about-sign-out/): what apps receive when a session ends.
- [Sign-in methods](../../../authentication/sign-in-methods/): removing a lost passkey or app.
