Skip to main content
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. 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:
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:
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:
Check the staging table before you go on, for example the number of rows in each partition:
Each partition needs its own REPLACE PARTITION statement. With many partitions, generate the statements from system.parts:
Replace the partitions and look at the result:
REPLACE PARTITION does not remove the data from the staging table, so drop it at the end:

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 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). 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, 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 or a mutation is less work.