> ## 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 reduce backup size

> What makes ClickHouse backups large and how to shrink them: retention, unused data, parts rewritten by mutations and merges, and column codecs, with measured examples.

The storage that backups use is roughly the product of three things: the size of the data, how long backups are kept, and how much of the data is rewritten between two backups. Work on them in the order of effort: retention is a setting, removing unused data is a decision, rewriting less means changing how you delete and update, and better compression needs measurements.

## Keep fewer backups

* In ClickHouse Cloud, the retention and the frequency of backups can be configured on the Scale and Enterprise tiers. See [configurable backups](https://clickhouse.com/docs/products/cloud/features/backups/overview.md#configurable-backups) for the allowed values and [understanding backup cost](https://clickhouse.com/docs/products/cloud/guides/backups/review-and-restore-backups.md#understanding-backup-cost). Backups form chains of one full backup and the incremental backups that follow it, and a chain is kept as long as one of its restore points is within the retention.
* If you [export backups to your own bucket](https://clickhouse.com/docs/products/cloud/features/backups/overview.md#byob), ClickHouse Cloud continues to take its own backups by default. Backups to your bucket that are configured in the Cloud console are full backups. Incremental ones need the `BACKUP` statement with the `base_backup` setting.
* With the [`BACKUP`](https://clickhouse.com/docs/concepts/features/backup-restore/overview) statement you manage the chain yourself. To create an incremental backup, ClickHouse reads only the `.backup` file of the base backup. To restore one, it reads the data files of the whole chain down to the full backup, so delete a backup only together with the backups that are based on it.

## Back up less data

Tables and partitions that nobody uses are backed up like everything else. This query lists the tables in which no part was written during the last 30 days, which finds abandoned copies and tables left over from migrations:

```sql theme={null}
SELECT
    database,
    table,
    formatReadableSize(sum(bytes_on_disk)) AS size,
    max(modification_time) AS last_modified
FROM system.parts
WHERE active AND database NOT IN ('system', 'INFORMATION_SCHEMA', 'information_schema')
GROUP BY database, table
HAVING last_modified < now() - INTERVAL 30 DAY
ORDER BY sum(bytes_on_disk) DESC;
```

Group by `partition` for one table to see how much old data it holds. Drop what is not needed, or let a [TTL](https://clickhouse.com/docs/concepts/features/operations/delete/ttl.md) delete or roll up old rows. The `BACKUP` statement can also leave tables out with `EXCEPT TABLES`.

## Avoid rewriting data between backups

An incremental backup stores every file that is not in its base backup with the same size and checksum. Data parts never change, but merges and mutations replace them with new parts, and those are stored again. The following test shows how much each kind of change costs. It writes to a disk named `backups`, configured as in [`BACKUP` / `RESTORE` to disk](https://clickhouse.com/docs/concepts/features/backup-restore/local-disk#configure-a-backup-destination). The two table settings make the small example behave like a large table: columns are stored in separate files, and the codec is `ZSTD(3)`, which parts of 100 MiB and more get by default.

```sql theme={null}
CREATE TABLE readings
(
    ts DateTime,
    sensor_id UInt32,
    value Float32,
    status LowCardinality(String)
)
ENGINE = MergeTree
PARTITION BY toYYYYMM(ts)
ORDER BY (sensor_id, ts)
SETTINGS min_bytes_for_wide_part = 0, default_compression_codec = 'ZSTD(3)';

INSERT INTO readings
SELECT
    toDateTime('2026-01-01 00:00:00') + number * 2 + cityHash64(number) % 600,
    number % 1000,
    round(20 + 10 * sin(number / 1000) + (cityHash64(number, 2) % 100) / 100, 2),
    ['ok', 'ok', 'ok', 'warn'][cityHash64(number, 1) % 4 + 1]
FROM numbers(4000000);

OPTIMIZE TABLE readings FINAL;

BACKUP TABLE readings TO Disk('backups', 'full');
```

An incremental backup based on the previous backup was then taken after each of the following changes, for example `BACKUP TABLE readings TO Disk('backups', 'inc_2_insert') SETTINGS base_backup = Disk('backups', 'inc_1_no_change')`. The first one, `inc_1_no_change`, was taken before any change:

```sql theme={null}
-- inc_2_insert: 1% more rows
INSERT INTO readings
SELECT toDateTime('2026-04-03 00:00:00') + number, number % 1000, 21.5, 'ok'
FROM numbers(40000);
-- inc_3_lightweight_delete
DELETE FROM readings WHERE sensor_id = 8;
-- inc_4_update_one_column: a mutation that changes one column in every row
ALTER TABLE readings UPDATE value = value + 1 WHERE 1 SETTINGS mutations_sync = 1;
-- inc_5_delete_in_one_month: a mutation that deletes one sensor in one partition
ALTER TABLE readings DELETE WHERE sensor_id = 7 AND ts < '2026-02-01' SETTINGS mutations_sync = 1;
-- inc_6_delete_in_all_months: a mutation that deletes one sensor everywhere
ALTER TABLE readings DELETE WHERE sensor_id = 9 SETTINGS mutations_sync = 1;
-- inc_7_insert_and_merge: 1% more rows in every month, then a merge
INSERT INTO readings
SELECT toDateTime('2026-01-01 00:00:00') + number * 200, number % 1000, 21.5, 'ok'
FROM numbers(40000);
OPTIMIZE TABLE readings FINAL;
```

`total_size` is what a restore brings back, `compressed_size` is what the backup wrote to the destination:

```sql theme={null}
SELECT
    name,
    formatReadableSize(total_size) AS restores,
    formatReadableSize(compressed_size) AS written
FROM system.backups
ORDER BY start_time;
```

```text theme={null}
   ┌─name──────────────────────────────────────────┬─restores──┬─written────┐
1. │ Disk('backups', 'full')                       │ 22.04 MiB │ 22.05 MiB  │
2. │ Disk('backups', 'inc_1_no_change')            │ 22.04 MiB │ 14.39 KiB  │
3. │ Disk('backups', 'inc_2_insert')               │ 22.15 MiB │ 128.88 KiB │
4. │ Disk('backups', 'inc_3_lightweight_delete')   │ 22.15 MiB │ 30.36 KiB  │
5. │ Disk('backups', 'inc_4_update_one_column')    │ 22.15 MiB │ 8.87 MiB   │
6. │ Disk('backups', 'inc_5_delete_in_one_month')  │ 22.14 MiB │ 7.38 MiB   │
7. │ Disk('backups', 'inc_6_delete_in_all_months') │ 22.10 MiB │ 22.12 MiB  │
8. │ Disk('backups', 'inc_7_insert_and_merge')     │ 22.34 MiB │ 22.35 MiB  │
   └───────────────────────────────────────────────┴───────────┴────────────┘
```

* An `ALTER TABLE ... UPDATE` stores the updated column again (`inc_4`). An `ALTER TABLE ... DELETE` stores every part that contains a matching row again in full: one partition in `inc_5`, and the whole table in `inc_6`. That delete removed only 0.1% of the rows, but every part has rows of that sensor.
* A merge creates a new part, so all the rows of the merged parts are stored again (`inc_7`). Background merges do the same to newly inserted data.
* A lightweight `DELETE` only added a small mask to each part (`inc_3`). The rows are removed, and the part is rewritten, by a later merge.

To keep incremental backups small, remove data by partition (`DROP PARTITION`, TTL) instead of by row where you can, limit a mutation to the partitions that need it with a condition on the partition key or with `IN PARTITION`, run row-level deletes in few large batches instead of many small ones, and do not run [`OPTIMIZE ... FINAL`](https://clickhouse.com/docs/concepts/best-practices/avoid-optimize-final) on a schedule. Parts of different partitions are never merged, so a partition that no longer receives data stops changing.

## Compress the largest columns better

First find where the bytes are:

```sql theme={null}
SELECT
    table,
    column,
    formatReadableSize(sum(column_data_compressed_bytes)) AS compressed,
    formatReadableSize(sum(column_data_uncompressed_bytes)) AS uncompressed,
    round(sum(column_data_uncompressed_bytes) / sum(column_data_compressed_bytes), 1) AS ratio
FROM system.parts_columns
WHERE active AND database = currentDatabase()
GROUP BY table, column
ORDER BY sum(column_data_compressed_bytes) DESC
LIMIT 20;
```

For the `readings` table as it was before the changes above:

```text theme={null}
   ┌─table────┬─column────┬─compressed─┬─uncompressed─┬─ratio─┐
1. │ readings │ ts        │ 12.47 MiB  │ 15.26 MiB    │   1.2 │
2. │ readings │ value     │ 8.82 MiB   │ 15.26 MiB    │   1.7 │
3. │ readings │ status    │ 704.05 KiB │ 3.82 MiB     │   5.6 │
4. │ readings │ sensor_id │ 26.01 KiB  │ 15.26 MiB    │ 600.7 │
   └──────────┴───────────┴────────────┴──────────────┴───────┘
```

Three columns have the same uncompressed size, and their compressed sizes differ by a factor of almost 500. What decides is the position in the sorting key: `sensor_id`, the first key column, consists of long runs of equal values. `ts`, the last key column, rises within each sensor, which a general-purpose codec can hardly use. Only `ts` and `value` are worth any work.

Measure a candidate codec on a sample before you change the table. `CREATE TABLE ... AS` keeps the sorting key, the partitioning and the settings identical, so that the codec is the only difference:

```sql theme={null}
CREATE TABLE codec_test_default AS readings;
CREATE TABLE codec_test_delta AS readings;
CREATE TABLE codec_test_t64 AS readings;

ALTER TABLE codec_test_delta
    MODIFY COLUMN ts DateTime CODEC(Delta, ZSTD(3)),
    MODIFY COLUMN value Float32 CODEC(Gorilla, ZSTD(3));
ALTER TABLE codec_test_t64
    MODIFY COLUMN ts DateTime CODEC(T64, ZSTD(3)),
    MODIFY COLUMN value Float32 CODEC(FPC, ZSTD(3));

INSERT INTO codec_test_default SELECT * FROM readings WHERE toYYYYMM(ts) = 202602;
INSERT INTO codec_test_delta SELECT * FROM readings WHERE toYYYYMM(ts) = 202602;
INSERT INTO codec_test_t64 SELECT * FROM readings WHERE toYYYYMM(ts) = 202602;

OPTIMIZE TABLE codec_test_default FINAL;
OPTIMIZE TABLE codec_test_delta FINAL;
OPTIMIZE TABLE codec_test_t64 FINAL;

SELECT
    table,
    column,
    formatReadableSize(sum(column_data_compressed_bytes)) AS compressed
FROM system.parts_columns
WHERE active AND database = currentDatabase() AND table LIKE 'codec_test_%' AND column IN ('ts', 'value')
GROUP BY table, column
ORDER BY column, table;
```

```text theme={null}
   ┌─table──────────────┬─column─┬─compressed─┐
1. │ codec_test_default │ ts     │ 3.82 MiB   │
2. │ codec_test_delta   │ ts     │ 2.07 MiB   │
3. │ codec_test_t64     │ ts     │ 2.30 MiB   │
4. │ codec_test_default │ value  │ 2.68 MiB   │
5. │ codec_test_delta   │ value  │ 3.69 MiB   │
6. │ codec_test_t64     │ value  │ 3.52 MiB   │
   └────────────────────┴────────┴────────────┘
```

[`Delta`](https://clickhouse.com/docs/reference/statements/create/table/codec#delta) makes the rising key column 46% smaller. For `value`, which changes from row to row, both specialized codecs are worse than plain `ZSTD(3)`, so that column is left alone. The numbers belong to this data: measure your own, and compare against the general-purpose codec that your parts really use (`default_compression_codec` in `system.parts`). Parts below 100 MiB are written with `LZ4` by default, so a small benchmark table without explicit codecs does not show what a large part would do.

Changing the codec of a column changes only the table metadata:

```sql theme={null}
ALTER TABLE readings MODIFY COLUMN ts DateTime CODEC(Delta, ZSTD(3));
```

The existing parts are not touched and keep their size. New inserts and merges use the new codec. To recompress existing data, rewrite it one partition at a time:

* For a column that is not part of the sorting or partition key, `ALTER TABLE ... UPDATE value = value IN PARTITION 202602 WHERE 1` rewrites that column only.
* A key column cannot be updated (`Cannot UPDATE key column`), so the parts have to be rewritten as a whole: `OPTIMIZE TABLE readings PARTITION 202601 FINAL`. In the example this reduced the partition from 7.42 MiB to 5.50 MiB. The next incremental backup wrote 2.30 MiB, about the size of the recompressed column, because the partition was a single part and the files of its other columns came out unchanged.

Changing the type of a column is different from changing its codec: it [rewrites the column](https://clickhouse.com/docs/reference/statements/alter/index.md#execution-cost-and-completion) in all parts.

See also [Compression in ClickHouse](https://clickhouse.com/docs/guides/clickhouse/data-modelling/compression/compression-in-clickhouse.md), the reference of the [column compression codecs](https://clickhouse.com/docs/reference/statements/create/table/codec.md) and [Avoid mutations](https://clickhouse.com/docs/concepts/best-practices/avoid-mutations.md).
