> ## 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.

# Why are rows missing after I delete them and insert them again?

> An INSERT that repeats an earlier block is skipped by insert deduplication, even if the rows were deleted in between. How to confirm it and how to insert the data again.

The most likely reason is insert deduplication. ClickHouse keeps the hashes of the last inserted blocks, and when an `INSERT` brings a block with exactly the same data, it treats it as a retry and skips it. Deleting the rows does not remove that hash, so the second `INSERT` reports success but writes nothing. To insert the data again, give the `INSERT` its own [`insert_deduplication_token`](https://clickhouse.com/docs/reference/settings/session-settings/insert#insert_deduplication_token).

## Example

Deduplication is on by default for replicated tables, including the tables of a ClickHouse Cloud service. A plain `MergeTree` table keeps no deduplication log unless you set `non_replicated_deduplication_window`, which this example does so that you can reproduce the behavior on a single server:

```sql theme={null}
CREATE TABLE orders
(
    id UInt32,
    status String
)
ENGINE = MergeTree
ORDER BY id
SETTINGS non_replicated_deduplication_window = 100;

INSERT INTO orders VALUES (1, 'new'), (2, 'new');
INSERT INTO orders VALUES (3, 'new');

DELETE FROM orders WHERE id IN (1, 2);

INSERT INTO orders VALUES (1, 'new'), (2, 'new');

SELECT * FROM orders ORDER BY id;
```

```text theme={null}
   ┌─id─┬─status─┐
1. │  3 │ new    │
   └────┴────────┘
```

The last `INSERT` repeats the first one byte for byte, so it is skipped. The rows `1` and `2` are not in the table.

## Confirm that the insert was deduplicated

The skipped block is recorded in [`system.part_log`](https://clickhouse.com/docs/reference/system-tables/part_log) as a `NewPart` event with the error `INSERT_WAS_DEDUPLICATED`:

```sql theme={null}
SYSTEM FLUSH LOGS;

SELECT
    part_name,
    rows,
    errorCodeToName(error) AS error_name,
    exception
FROM system.part_log
WHERE database = currentDatabase() AND table = 'orders' AND event_type = 'NewPart'
ORDER BY event_time_microseconds;
```

```text theme={null}
   ┌─part_name─┬─rows─┬─error_name──────────────┬─exception─────────────────┐
1. │ all_1_1_0 │    2 │ OK                      │                           │
2. │ all_2_2_0 │    1 │ OK                      │                           │
3. │ all_4_4_0 │    2 │ INSERT_WAS_DEDUPLICATED │ The part was deduplicated │
   └───────────┴──────┴─────────────────────────┴───────────────────────────┘
```

The `DuplicatedInsertedBlocks` profile event of the `INSERT` query in `system.query_log` is greater than zero as well.

## Insert the data again

Pass a deduplication token that you have not used before. With a token, ClickHouse compares the token instead of the hash of the data:

```sql theme={null}
INSERT INTO orders SETTINGS insert_deduplication_token = 'reload-2026-12-01' VALUES (1, 'new'), (2, 'new');

SELECT * FROM orders ORDER BY id;
```

```text theme={null}
   ┌─id─┬─status─┐
1. │  1 │ new    │
2. │  2 │ new    │
3. │  3 │ new    │
   └────┴────────┘
```

A token keeps the protection against retries: if the same statement is sent twice with the same token, the second one is skipped. For the same reason, do not reuse a token for different data.

Alternatively, turn deduplication off for that one statement. A retry of such an `INSERT` writes the rows twice:

```sql theme={null}
INSERT INTO orders SETTINGS deduplicate_insert = 'disable' VALUES (1, 'new'), (2, 'new');
```

> [!NOTE]
>
> Since ClickHouse 26.2, [`deduplicate_insert`](https://clickhouse.com/docs/reference/settings/session-settings/deduplicate-insert#deduplicate_insert) is `enable` by default, and `SET insert_deduplicate = 0` alone no longer turns deduplication off. On earlier releases use `insert_deduplicate = 0`.

## What decides whether a block is a duplicate

* The hash covers a whole inserted block. An `INSERT` with different rows, a different number of rows, or one changed value is not a duplicate. In the example, inserting only the row `(1, 'new')` would have worked.
* `ALTER TABLE ... DELETE` and `DELETE FROM` leave the stored hashes in place. `TRUNCATE TABLE` and `ALTER TABLE ... DROP PARTITION` remove the hashes of the data they drop, so the same data can be inserted again after them.
* Replicated tables keep the hashes of the most recent [`replicated_deduplication_window`](https://clickhouse.com/docs/reference/settings/merge-tree-settings/replicated-deduplication-window#replicated_deduplication_window) blocks (10000 by default) and drop hashes older than [`replicated_deduplication_window_seconds`](https://clickhouse.com/docs/reference/settings/merge-tree-settings/replicated-deduplication-window#replicated_deduplication_window_seconds) (one hour by default). An identical block inserted after its hash has left the window is accepted.
* Non-replicated `MergeTree` tables deduplicate only if [`non_replicated_deduplication_window`](https://clickhouse.com/docs/reference/settings/merge-tree-settings/other#non_replicated_deduplication_window) is set. It is `0` by default.

For the full description, see [Deduplicating inserts on retries](https://clickhouse.com/docs/concepts/features/operations/insert/deduplicating-inserts-on-retries.md).

## Another cause: reading from a replica that is behind

If the service has several replicas and the `DELETE`, the `INSERT` and the `SELECT` do not all go to the same replica, the `SELECT` can run on a replica that has not received the new rows yet. They appear a moment later without any action. See [How to achieve data read consistency in ClickHouse](https://clickhouse.com/docs/resources/support-center/knowledge-base/data-management/read-consistency.md) for ways to read your own writes.
