> ## Documentation Index
> Fetch the complete documentation index at: https://cockroachlabs.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# CANCEL SESSION

export const InternalLink = ({version, path = "", children, ...props}) => {
  let detectedVersion = version || "stable";
  if (typeof window !== 'undefined' && !version) {
    const match = window.location.pathname.match(/\/docs\/([^/]+)/);
    if (match) {
      detectedVersion = match[1];
    }
  }
  const normalizedPath = path.startsWith("/") ? path.slice(1) : path;
  return <a href={`/docs/${detectedVersion}/${normalizedPath}`} {...props}>
      {children}
    </a>;
};

The `CANCEL SESSION` <InternalLink path="sql-statements">statement</InternalLink> lets you stop long-running sessions. `CANCEL SESSION` will attempt to cancel the currently active query and end the session.

## Required privileges

To view and cancel a session, the user must be a member of the `admin` role or must have the `VIEWACTIVITY` <InternalLink path="security-reference/authorization#supported-privileges">system privilege</InternalLink> (or the legacy <InternalLink path="create-user#create-a-user-that-can-see-and-cancel-non-admin-queries-and-sessions">`VIEWACTIVITY`</InternalLink> <InternalLink path="security-reference/authorization">role option</InternalLink>) and the `CANCELQUERY` <InternalLink path="security-reference/authorization#supported-privileges">system privilege</InternalLink> (or the legacy <InternalLink path="create-user#create-a-user-that-can-see-and-cancel-non-admin-queries-and-sessions">`CANCELQUERY`</InternalLink> <InternalLink path="security-reference/authorization">role option</InternalLink>) defined. Non-admin users cannot cancel admin users' sessions.

## Synopsis

<img src="https://mintcdn.com/cockroachlabs/9gyQKEP-CuQuCsI3/images/sql-diagrams/v25.3/cancel_session.svg?fit=max&auto=format&n=9gyQKEP-CuQuCsI3&q=85&s=48c7c03b81cb2dc8b2fc8934b89b8c74" alt="cancel_session syntax diagram" style={{maxWidth: "100%", overflowX: "auto"}} width="583" height="145" data-path="images/sql-diagrams/v25.3/cancel_session.svg" />

## Parameters

| Parameter     | Description                                                                                                                                                                                                                                                                                                                                 |
| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `session_id`  | The ID of the session you want to cancel, which can be found with <InternalLink path="show-sessions">`SHOW SESSIONS`</InternalLink>.<br /><br />`CANCEL SESSION` accepts a single session ID. If a subquery is used and returns multiple IDs, the `CANCEL SESSION` statement will fail. To cancel multiple sessions, use `CANCEL SESSIONS`. |
| `select_stmt` | A <InternalLink path="selection-queries">selection query</InternalLink> that returns `session_id`(s) to cancel.                                                                                                                                                                                                                             |

## Example

### Cancel a single session

In this example, we use the <InternalLink path="show-sessions">`SHOW SESSIONS`</InternalLink> statement to get the ID of a session and then pass the ID into the `CANCEL SESSION` statement:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> SHOW SESSIONS;
```

```
+---------+----------------------------------+-----------+...
| node_id |            session_id            | user_name |...
+---------+----------------------------------+-----------+...
|       1 | 1530c309b1d8d5f00000000000000001 | root      |...
+---------+----------------------------------+-----------+...
|       1 | 1530fe0e46d2692e0000000000000001 | maxroach  |...
+---------+----------------------------------+-----------+...
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> CANCEL SESSION '1530fe0e46d2692e0000000000000001';
```

You can also cancel a session using a subquery that returns a single session ID:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> CANCEL SESSIONS (WITH x AS (SHOW SESSIONS) SELECT session_id FROM x
      WHERE user_name = 'root');
```

### Cancel multiple sessions

Use the <InternalLink path="show-sessions">`SHOW SESSIONS`</InternalLink> statement to view all active sessions:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> SHOW SESSIONS;
```

```
+---------+----------------------------------+-----------+...
| node_id |            session_id            | user_name |...
+---------+----------------------------------+-----------+...
|       1 | 1530c309b1d8d5f00000000000000001 | root      |...
+---------+----------------------------------+-----------+...
|       1 | 1530fe0e46d2692e0000000000000001 | maxroach  |...
+---------+----------------------------------+-----------+...
|       1 | 15310cc79671fc6a0000000000000001 | maxroach  |...
+---------+----------------------------------+-----------+...
```

To cancel multiple sessions, nest a <InternalLink path="select-clause">`SELECT` clause</InternalLink> that retrieves `session_id`(s) inside the `CANCEL SESSIONS` statement:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> CANCEL SESSIONS (WITH x AS (SHOW SESSIONS) SELECT session_id FROM x
      WHERE user_name = 'maxroach');
```

All sessions created by `maxroach` will be cancelled.

## See also

* <InternalLink path="show-sessions">`SHOW SESSIONS`</InternalLink>
* <InternalLink path="set-vars">`SET session variable`</InternalLink>
* <InternalLink path="show-vars">`SHOW session variable`</InternalLink>
* <InternalLink path="sql-statements">SQL Statements</InternalLink>
