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

# CANCEL JOB

The `CANCEL JOB` [statement](https://docs.cockroachlabs.com/docs/stable/sql-statements.md) lets you stop long-running jobs, which include:

* [`IMPORT`](https://docs.cockroachlabs.com/docs/stable/import-into) jobs
* [`BACKUP`](https://docs.cockroachlabs.com/docs/stable/backup) and [`RESTORE`](https://docs.cockroachlabs.com/docs/stable/restore) jobs
* [User-created table statistics](https://docs.cockroachlabs.com/docs/stable/create-statistics.md) jobs
* [Automatic table statistics](https://docs.cockroachlabs.com/docs/stable/cost-based-optimizer.md#table-statistics) jobs
* [Changefeeds](https://docs.cockroachlabs.com/docs/stable/create-changefeed.md)
* [Scheduled backup](https://docs.cockroachlabs.com/docs/stable/manage-a-backup-schedule.md) jobs
* [Schema change](https://docs.cockroachlabs.com/docs/stable/online-schema-changes.md) jobs (see [Known limitations](#known-limitations) for exceptions)

## Known limitations

* To avoid transaction states that cannot properly [roll back](https://docs.cockroachlabs.com/docs/stable/rollback-transaction.md), the following statements cannot be cancelled with [`CANCEL JOB`](https://docs.cockroachlabs.com/docs/stable/cancel-job):

  * `DROP` statements (e.g., [`DROP TABLE`](https://docs.cockroachlabs.com/docs/stable/drop-table)).
  * `ALTER ... RENAME` statements (e.g., [`ALTER TABLE ... RENAME TO`](https://docs.cockroachlabs.com/docs/stable/alter-table#rename-to)).
  * [`CREATE TABLE ... AS`](https://docs.cockroachlabs.com/docs/stable/create-table-as) statements.
  * [`ALTER TYPE`](https://docs.cockroachlabs.com/docs/stable/alter-type) statements, except for those that drop values.

* When an Enterprise [`RESTORE`](https://docs.cockroachlabs.com/docs/stable/restore) is canceled, partially restored data is properly cleaned up. This can have a minor, temporary impact on cluster performance.

## Required privileges

To cancel a job, the user must be a member of the `admin` role or must have the [`CONTROLJOB`](https://docs.cockroachlabs.com/docs/stable/create-user#create-a-user-that-can-pause-resume-and-cancel-non-admin-jobs) [role option](https://docs.cockroachlabs.com/docs/stable/security-reference/authorization.md) set. Non-admin users cannot cancel admin users' jobs.

For changefeeds, users with the [`CHANGEFEED`](https://docs.cockroachlabs.com/docs/stable/create-changefeed#required-privileges) privilege on a set of tables can cancel changefeed jobs running on those tables.

## Synopsis

![cancel\_job syntax diagram](https://mintcdn.com/cockroachlabs/VQ_w3c6MHYdiS7Tn/images/sql-diagrams/common/cancel_job.svg?fit=max\&auto=format\&n=VQ_w3c6MHYdiS7Tn\&q=85\&s=52fc028682b0d4c2db7b6f9cea1f36d0)

### Cancel all jobs by type

![cancel\_all\_jobs syntax diagram](https://mintcdn.com/cockroachlabs/7gzGESYW_aT0XysP/images/sql-diagrams/v26.3/cancel_all_jobs.svg?fit=max\&auto=format\&n=7gzGESYW_aT0XysP\&q=85\&s=20feba3653f6cdf151f5d809504fc5c7)

## Parameters

| Parameter | Description |
| - | - |
| `job_id` | The ID of the job you want to cancel, which can be found with [`SHOW JOBS`](https://docs.cockroachlabs.com/docs/stable/show-jobs). |
| `select_stmt` | A [selection query](https://docs.cockroachlabs.com/docs/stable/selection-queries.md) that returns `job_id`(s) to cancel. |
| `for_schedules_clause` | The schedule you want to cancel jobs for. You can cancel jobs for a specific schedule (`FOR SCHEDULE id`) or cancel jobs for multiple schedules by nesting a [`SELECT` clause](https://docs.cockroachlabs.com/docs/stable/select-clause) in the statement (`FOR SCHEDULES <select_clause>`). See the [examples](#cancel-jobs-for-a-schedule) below. |
| `BACKUP`, `CHANGEFEED`, `RESTORE`, `IMPORT` | The job type to cancel. |

## Examples

### Cancel a single job

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

```
+----------------+---------+------------------------------------------------+...
|       id       |  type   |               description                      |...
+----------------+---------+------------------------------------------------+...
| 27536791415282 | RESTORE | RESTORE db.* FROM 'azure-blob://backup/db/tbl' |...
+----------------+---------+------------------------------------------------+...
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> CANCEL JOB 27536791415282;
```

### Cancel multiple jobs

To cancel multiple jobs, nest a [`SELECT` clause](https://docs.cockroachlabs.com/docs/stable/select-clause) that retrieves `job_id`(s) inside the `CANCEL JOBS` statement:

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

All jobs created by `maxroach` will be cancelled.

### Cancel by job type

To cancel all jobs by the type of job, use the `CANCEL ALL {job} JOBS` statement. You can cancel all `BACKUP`, `RESTORE`, `CHANGEFEED`, `IMPORT` jobs using this statement, for example:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
CANCEL ALL BACKUP JOBS;
```

### Cancel automatic table statistics jobs

Canceling an automatic table statistics job is not useful since the system will automatically restart the job immediately. To permanently disable automatic table statistics jobs, disable the `sql.stats.automatic_collection.enabled` [cluster setting](https://docs.cockroachlabs.com/docs/stable/cluster-settings.md):

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> SET CLUSTER SETTING sql.stats.automatic_collection.enabled = false;
```

### Cancel jobs for a schedule

To cancel jobs for a specific [backup schedule](https://docs.cockroachlabs.com/docs/stable/create-schedule-for-backup.md), use the schedule's `id`:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> CANCEL JOBS FOR SCHEDULE 590204387299262465;
```

```
CANCEL JOBS FOR SCHEDULES 1
```

You can also CANCEL multiple schedules by nesting a [`SELECT` clause](https://docs.cockroachlabs.com/docs/stable/select-clause) that retrieves `id`(s) inside the `CANCEL JOBS` statement:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> CANCEL JOBS FOR SCHEDULES WITH x AS (SHOW SCHEDULES) SELECT id FROM x WHERE label = 'test_schedule';
```

```
CANCEL JOBS FOR SCHEDULES 2
```

## See also

* [`SHOW JOBS`](https://docs.cockroachlabs.com/docs/stable/show-jobs)
* [`BACKUP`](https://docs.cockroachlabs.com/docs/stable/backup)
* [`RESTORE`](https://docs.cockroachlabs.com/docs/stable/restore)
* [`IMPORT INTO`](https://docs.cockroachlabs.com/docs/stable/import-into)
* [`CREATE CHANGEFEED`](https://docs.cockroachlabs.com/docs/stable/create-changefeed)
