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

# How to find the oldest unfinished mutation and kill stuck mutations

> Use system.mutations to get the age of the oldest unfinished mutation, read latest_fail_reason to see why it cannot finish, and remove it with KILL MUTATION.

Every mutation, such as `ALTER TABLE ... UPDATE` or `ALTER TABLE ... DELETE`, has a row in [`system.mutations`](https://clickhouse.com/docs/reference/system-tables/mutations) that stays there with `is_done = 0` until all parts are rewritten. The age of the oldest such row tells you whether mutations are keeping up, `latest_fail_reason` tells you why a mutation cannot finish, and [`KILL MUTATION`](https://clickhouse.com/docs/reference/statements/kill#kill-mutation) removes it.

## Age of the oldest unfinished mutation

```sql theme={null}
SELECT
    database,
    table,
    count() AS unfinished_mutations,
    min(create_time) AS oldest_create_time,
    formatReadableTimeDelta(dateDiff('second', min(create_time), now())) AS oldest_age
FROM system.mutations
WHERE NOT is_done
GROUP BY
    database,
    table
ORDER BY oldest_create_time;
```

How long a mutation takes depends on the amount of data it has to rewrite, so there is no universal threshold for the age. A mutation whose `parts_to_do` does not decrease, or that has a non-empty `latest_fail_reason`, needs a closer look. The number of unfinished mutations alone is covered in [How do I view the number of active or queued mutations?](https://clickhouse.com/docs/resources/support-center/knowledge-base/monitoring-debugging/view-number-of-active-mutations.md).

## Example: a mutation that can never finish

Create a table with one value that the mutation cannot process:

```sql theme={null}
CREATE TABLE orders
(
    id UInt64,
    status String,
    amount String
)
ENGINE = MergeTree
ORDER BY id;

INSERT INTO orders VALUES (1, 'new', '10'), (2, 'new', '20'), (3, 'paid', 'n/a');

ALTER TABLE orders
    UPDATE status = 'large' WHERE toUInt32(amount) > 15
SETTINGS mutations_sync = 1;
```

With `mutations_sync = 1` the statement waits for the mutation, so it reports the failure (the message is shortened here; the full text also contains a stack trace):

```text theme={null}
Code: 341. DB::Exception: Exception happened during execution of mutation 'mutation_2.txt' with part 'all_1_1_0' reason: 'Code: 6. DB::Exception: Cannot parse string 'n/a' as UInt32: syntax error at begin of string. ... (CANNOT_PARSE_TEXT) ...'. This error maybe retryable or not. In case of unretryable error, mutation can be killed with KILL MUTATION query ... (UNFINISHED)
```

The exception does not cancel the mutation. It stays in the queue and the server keeps retrying it. With the default `mutations_sync = 0` the `ALTER` returns immediately and you see no error at all, only a mutation that never completes.

List the unfinished mutations together with the reason of the last failure:

```sql theme={null}
SELECT
    database,
    table,
    mutation_id,
    command,
    parts_to_do,
    latest_failed_part,
    latest_fail_reason
FROM system.mutations
WHERE NOT is_done
ORDER BY create_time
FORMAT Vertical;
```

```text theme={null}
Row 1:
──────
database:           default
table:              orders
mutation_id:        mutation_2.txt
command:            (UPDATE status = 'large' WHERE toUInt32(amount) > 15)
parts_to_do:        1
latest_failed_part: all_1_1_0
latest_fail_reason: Code: 6. DB::Exception: Cannot parse string 'n/a' as UInt32: syntax error at begin of string. Note: there are toUInt32OrZero and toUInt32OrNull functions, which returns zero/NULL instead of throwing exception: while executing 'FUNCTION toUInt32(amount :: 0) -> toUInt32(__table1.amount) UInt32 : 2'. (CANNOT_PARSE_TEXT) (version 26.12.1.1315 (official build))
```

An empty `latest_fail_reason` means that the mutation has not failed: it is waiting or still running. To list only old mutations, add a condition such as `AND create_time < now() - INTERVAL 1 DAY`.

## A stuck mutation blocks the ones behind it

Mutations of a table are applied to each part in the order of their creation, so a valid mutation submitted later cannot complete either:

```sql theme={null}
ALTER TABLE orders
    UPDATE status = 'archived' WHERE id = 1;

SELECT
    mutation_id,
    command,
    is_done
FROM system.mutations
WHERE database = 'default' AND table = 'orders'
ORDER BY mutation_id;
```

```text theme={null}
   ┌─mutation_id────┬─command───────────────────────────────────────────────┬─is_done─┐
1. │ mutation_2.txt │ (UPDATE status = 'large' WHERE toUInt32(amount) > 15) │       0 │
2. │ mutation_3.txt │ (UPDATE status = 'archived' WHERE id = 1)             │       0 │
   └────────────────┴───────────────────────────────────────────────────────┴─────────┘
```

## Kill the stuck mutation

`KILL MUTATION` takes a `WHERE` clause over the columns of `system.mutations`. Select exactly the mutation you want to remove:

```sql theme={null}
KILL MUTATION
WHERE database = 'default' AND table = 'orders' AND mutation_id = 'mutation_2.txt';
```

```text theme={null}
   ┌─kill_status─┬─database─┬─table──┬─mutation_id────┬─command───────────────────────────────────────────────┐
1. │ waiting     │ default  │ orders │ mutation_2.txt │ (UPDATE status = 'large' WHERE toUInt32(amount) > 15) │
   └─────────────┴──────────┴────────┴────────────────┴───────────────────────────────────────────────────────┘
```

A few seconds later, the same query on `system.mutations` shows that the killed mutation is gone and the one that was waiting behind it is done:

```text theme={null}
   ┌─mutation_id────┬─command───────────────────────────────────┬─is_done─┐
1. │ mutation_3.txt │ (UPDATE status = 'archived' WHERE id = 1) │       1 │
   └────────────────┴───────────────────────────────────────────┴─────────┘
```

```sql theme={null}
SELECT * FROM orders ORDER BY id;
```

```text theme={null}
   ┌─id─┬─status───┬─amount─┐
1. │  1 │ archived │ 10     │
2. │  2 │ new      │ 20     │
3. │  3 │ paid     │ n/a    │
   └────┴──────────┴────────┘
```

Keep in mind:

* `KILL MUTATION` does not roll back the parts that the mutation has already rewritten. If the mutation had been applied to some parts, those keep the new data.
* Add `TEST` after the `WHERE` clause to list the mutations that the statement would kill without killing them.
* Fix the cause before you submit the mutation again. Here, `toUInt32OrZero(amount)` instead of `toUInt32(amount)` makes the same update succeed.

## Clusters and ClickHouse Cloud

On a self-managed cluster `system.mutations` shows the mutations known to the server that runs the query. To see all replicas, read the table through `clusterAllReplicas`, replacing `default` with the name of your cluster:

```sql theme={null}
SELECT
    hostName() AS host,
    database,
    table,
    count() AS unfinished_mutations,
    min(create_time) AS oldest_create_time
FROM clusterAllReplicas('default', system.mutations)
WHERE NOT is_done
GROUP BY
    host,
    database,
    table
ORDER BY oldest_create_time;
```

A mutation of a `ReplicatedMergeTree` table is stored in ClickHouse Keeper and is shared by all replicas of the table, so killing it on one replica removes it for all of them. In ClickHouse Cloud, `system.mutations` on each node already contains all the mutations of the service, as described in the [`system.mutations` reference](https://clickhouse.com/docs/reference/system-tables/mutations#monitoring-mutations).
