> ## 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 delete or correct rows in bulk with REPLACE PARTITION instead of a mutation

> Remove or fix many rows of a partitioned MergeTree table without a mutation: rebuild the affected partitions in a staging table and swap them in with REPLACE PARTITION.

To remove or correct many rows of a partitioned `MergeTree` table without an `ALTER TABLE ... DELETE` or `ALTER TABLE ... UPDATE` mutation, write the rows you want to keep, already corrected, into a staging table with the same structure, and swap whole partitions with [`ALTER TABLE ... REPLACE PARTITION`](https://clickhouse.com/docs/reference/statements/alter/partition#replace-partition). The expensive part is an ordinary `INSERT ... SELECT` that you can check and repeat before the production table is touched, and each partition is then replaced atomically.

## Example

The `events` table is partitioned by month:

```sql theme={null}
CREATE TABLE events
(
    event_date Date,
    user_id UInt32,
    action LowCardinality(String),
    payload String
)
ENGINE = MergeTree
PARTITION BY toYYYYMM(event_date)
ORDER BY (user_id, event_date);

INSERT INTO events
SELECT
    toDate('2026-01-01') + number % 90,
    number % 997,
    ['view', 'click', 'purchase'][number % 3 + 1],
    concat('payload-', toString(number))
FROM numbers(900000);
```

In January and February, all rows of users `17` and `42` have to be deleted, and the `payload` of user `99` has to be emptied:

```sql theme={null}
SELECT
    toYYYYMM(event_date) AS month,
    count() AS rows,
    countIf(user_id IN (17, 42)) AS rows_to_delete,
    countIf(user_id = 99 AND payload != '') AS rows_to_correct
FROM events
GROUP BY month
ORDER BY month;
```

```text theme={null}
   ┌──month─┬───rows─┬─rows_to_delete─┬─rows_to_correct─┐
1. │ 202601 │ 310000 │            622 │             313 │
2. │ 202602 │ 280000 │            564 │             280 │
3. │ 202603 │ 310000 │            620 │             310 │
   └────────┴────────┴────────────────┴─────────────────┘
```

Create the staging table with `CREATE TABLE ... AS`, which copies the columns, the keys, the indexes, the projections and the settings. Fill it with the cleaned version of the affected partitions:

```sql theme={null}
CREATE TABLE events_staging AS events;

INSERT INTO events_staging
SELECT * REPLACE (if(user_id = 99, '', payload) AS payload)
FROM events
WHERE event_date < '2026-03-01'
  AND user_id NOT IN (17, 42);
```

Check the staging table before you go on, for example the number of rows in each partition:

```sql theme={null}
SELECT
    partition_id,
    sum(rows) AS rows
FROM system.parts
WHERE database = currentDatabase() AND table = 'events_staging' AND active
GROUP BY partition_id
ORDER BY partition_id;
```

```text theme={null}
   ┌─partition_id─┬───rows─┐
1. │ 202601       │ 309378 │
2. │ 202602       │ 279436 │
   └──────────────┴────────┘
```

Each partition needs its own `REPLACE PARTITION` statement. With many partitions, generate the statements from `system.parts`:

```sql theme={null}
SELECT DISTINCT
    format('ALTER TABLE events REPLACE PARTITION ID \'{}\' FROM events_staging;', partition_id) AS statement
FROM system.parts
WHERE database = currentDatabase() AND table = 'events_staging' AND active
ORDER BY statement;
```

```text theme={null}
   ┌─statement─────────────────────────────────────────────────────────────┐
1. │ ALTER TABLE events REPLACE PARTITION ID '202601' FROM events_staging; │
2. │ ALTER TABLE events REPLACE PARTITION ID '202602' FROM events_staging; │
   └───────────────────────────────────────────────────────────────────────┘
```

Replace the partitions and look at the result:

```sql theme={null}
ALTER TABLE events REPLACE PARTITION ID '202601' FROM events_staging;
ALTER TABLE events REPLACE PARTITION ID '202602' FROM events_staging;

SELECT
    toYYYYMM(event_date) AS month,
    count() AS rows,
    countIf(user_id IN (17, 42)) AS rows_to_delete,
    countIf(user_id = 99 AND payload != '') AS rows_to_correct
FROM events
GROUP BY month
ORDER BY month;
```

```text theme={null}
   ┌──month─┬───rows─┬─rows_to_delete─┬─rows_to_correct─┐
1. │ 202601 │ 309378 │              0 │               0 │
2. │ 202602 │ 279436 │              0 │               0 │
3. │ 202603 │ 310000 │            620 │             310 │
   └────────┴────────┴────────────────┴─────────────────┘
```

`REPLACE PARTITION` does not remove the data from the staging table, so drop it at the end:

```sql theme={null}
DROP TABLE events_staging;
```

## What it requires

* Both tables must have the same structure, partition key, sorting key, primary key and storage policy, and the production table must have every index and projection of the staging table. The full list is in the [`REPLACE PARTITION`](https://clickhouse.com/docs/reference/statements/alter/partition#replace-partition) reference. A mismatch is reported as an error such as `Tables have different ordering`, and nothing is replaced.
* The staging table must have data in the partition. Since version 26.6, replacing a partition from a source that has no parts in it fails with `BAD_ARGUMENTS` instead of silently emptying the partition (see [`allow_replace_partition_from_empty_source`](https://clickhouse.com/docs/reference/settings/session-settings/allow#allow_replace_partition_from_empty_source)). If no row of a partition is to be kept, use `ALTER TABLE ... DROP PARTITION`.
* You need disk space for a second copy of the rows you keep until the old parts are gone. The swap itself does not copy data when both tables are on the same local disk: the files of the staging parts are hard-linked into the production table. The replaced parts become inactive and are deleted from disk after [`old_parts_lifetime`](https://clickhouse.com/docs/reference/settings/merge-tree-settings/other#old_parts_lifetime), which is 480 seconds by default.

## What to watch out for

* **Rows inserted after the copy are lost.** The partition is replaced with what the staging table contains. A row that is inserted into the production partition between the `INSERT ... SELECT` and the `REPLACE PARTITION` disappears. Use the method for partitions that no longer receive data, or stop the writes to them for the duration.
* **Atomic for a partition, not for the set.** Each partition is replaced atomically, but the partitions are replaced one after another. A query that runs in between sees some partitions in the new state and some in the old one.
* **Materialized views are not updated.** Materialized views are triggered by inserts into their source table. Neither the insert into the staging table nor `REPLACE PARTITION` triggers the views of the production table, so their target tables still contain data derived from the removed or changed rows.
* **The whole partition is rewritten.** The method fits changes that affect many rows, or rows scattered across a partition. For a small number of rows, a [lightweight `DELETE`](https://clickhouse.com/docs/concepts/features/operations/delete/lightweight-delete) or a mutation is less work.

## Related pages

* [Delete overview](https://clickhouse.com/docs/concepts/features/operations/delete/overview.md) compares the other ways to delete data.
* [Avoid mutations](https://clickhouse.com/docs/concepts/best-practices/avoid-mutations.md) explains what a mutation costs.
* [Achieving atomic inserts and multi-table consistency](https://clickhouse.com/docs/resources/support-center/knowledge-base/data-import-export/achieving-atomic-inserts.md) uses staging tables and partition operations to load new data.
