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

# Unused tables keep their primary key in memory: how to find and unload them

> Find tables that nobody queries but that still hold their primary index in memory, release the memory with SYSTEM UNLOAD PRIMARY KEY, and keep it released.

A `MergeTree` table keeps the primary index of its data parts in memory once it is loaded, whether or not anyone still queries the table. Tables left behind by a rename-based migration (`sessions_old`, `events_backup`) are the usual case: the new table is small, the old one still holds its whole index. Find such tables with the `primary_key_bytes_in_memory` column of [`system.parts`](https://clickhouse.com/docs/reference/system-tables/parts), release the memory with [`SYSTEM UNLOAD PRIMARY KEY`](https://clickhouse.com/docs/reference/statements/system#unload-primary-key), and drop the tables when you no longer need them.

## Example

Create a table, fill it, then migrate to a new sorting key by renaming the old table and creating a new one:

```sql theme={null}
CREATE TABLE sessions
(
    session_id String,
    started_at DateTime,
    duration_ms UInt32
)
ENGINE = MergeTree
ORDER BY (session_id, started_at);

INSERT INTO sessions
SELECT
    hex(sipHash128(number)),
    toDateTime('2026-01-01 00:00:00') + number,
    number % 1000
FROM numbers(1000000);

RENAME TABLE sessions TO sessions_old;

CREATE TABLE sessions AS sessions_old
ENGINE = MergeTree
ORDER BY (started_at, session_id);

INSERT INTO sessions
SELECT *
FROM sessions_old
WHERE started_at >= '2026-01-12 00:00:00';
```

Break the memory used by primary keys down by exact table name:

```sql theme={null}
SELECT
    database,
    `table`,
    formatReadableSize(sum(primary_key_bytes_in_memory)) AS pk_in_memory,
    sum(rows) AS rows,
    sum(marks) AS marks
FROM system.parts
WHERE active
GROUP BY
    database,
    `table`
ORDER BY sum(primary_key_bytes_in_memory) DESC
LIMIT 20;
```

```text theme={null}
   ┌─database─┬─table────────┬─pk_in_memory─┬────rows─┬─marks─┐
1. │ default  │ sessions_old │ 4.84 KiB     │ 1000000 │   124 │
2. │ default  │ sessions     │ 308.00 B     │   49600 │     7 │
   └──────────┴──────────────┴──────────────┴─────────┴───────┘
```

The renamed table still holds its index. The numbers are tiny here because the table is tiny: the index has one entry per mark, so its size grows with the number of marks and with the width of the key columns.

Unload the primary key of the table that is no longer used, and run the query again:

```sql theme={null}
SYSTEM UNLOAD PRIMARY KEY default.sessions_old;
```

```text theme={null}
   ┌─database─┬─table────────┬─pk_in_memory─┬────rows─┬─marks─┐
1. │ default  │ sessions     │ 308.00 B     │   49600 │     7 │
2. │ default  │ sessions_old │ 0.00 B       │ 1000000 │   124 │
   └──────────┴──────────────┴──────────────┴─────────┴───────┘
```

The statement only frees the in-memory copy of the index. It doesn't change the data on disk, and the table stays readable.

## Why the memory is not released on its own

* A part written by an `INSERT` or by a merge keeps the index it has built in memory.
* After a server restart, the [`primary_key_lazy_load`](https://clickhouse.com/docs/reference/settings/merge-tree-settings/primary-key#primary_key_lazy_load) setting (enabled by default) loads the index of a part when a query first needs it, so a table that nobody queries starts with nothing loaded.
* Once loaded, the index of an active part stays in memory. `RENAME TABLE` doesn't touch the parts, so the renamed table keeps everything it had loaded.

When you look for candidates, group by the exact table name as in the query above. A filter or a roll-up by a name pattern adds the old and the new table together and hides which of them holds the memory.

## The index is loaded again on the next use

Unloading is not permanent. A query that filters on the key loads the index again:

```sql theme={null}
SELECT count()
FROM sessions_old
WHERE session_id = 'no such session';
```

After this query the diagnostic query shows the old table at the top again:

```text theme={null}
   ┌─database─┬─table────────┬─pk_in_memory─┬────rows─┬─marks─┐
1. │ default  │ sessions_old │ 4.84 KiB     │ 1000000 │   124 │
2. │ default  │ sessions     │ 308.00 B     │   49600 │     7 │
   └──────────┴──────────────┴──────────────┴─────────┴───────┘
```

Parts created by later merges of the table hold their index as well. To keep the memory released, drop the table once you have confirmed that nothing reads it and that any backfill from it is complete:

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

## Clusters and privileges

`SYSTEM UNLOAD PRIMARY KEY` acts on the server that executes it. On a cluster, run it on every replica or add `ON CLUSTER`:

```sql theme={null}
SYSTEM UNLOAD PRIMARY KEY default.sessions_old ON CLUSTER my_cluster;
```

To see the memory per replica, read `system.parts` through `clusterAllReplicas` and add `hostName()` to the grouping. In ClickHouse Cloud the cluster name is `default`, as described in [Querying across nodes](https://clickhouse.com/docs/products/cloud/features/monitoring/system-tables.md#querying-across-nodes):

```sql theme={null}
SELECT
    hostName() AS host,
    database,
    `table`,
    formatReadableSize(sum(primary_key_bytes_in_memory)) AS pk_in_memory
FROM clusterAllReplicas(default, system.parts)
WHERE active
GROUP BY
    host,
    database,
    `table`
ORDER BY sum(primary_key_bytes_in_memory) DESC
LIMIT 20;
```

The statement needs the `SYSTEM UNLOAD PRIMARY KEY` privilege, which can be granted for one table:

```sql theme={null}
GRANT SYSTEM UNLOAD PRIMARY KEY ON default.sessions_old TO my_user;
```

Without a table name, `SYSTEM UNLOAD PRIMARY KEY` unloads the primary keys of all tables and needs the privilege globally. The statement is available since ClickHouse 24.4.

## Large tables that are still in use

Unloading helps for tables that are not read. For a table that is queried and has a large primary index, look at the [`use_primary_key_cache`](https://clickhouse.com/docs/reference/settings/merge-tree-settings/use#use_primary_key_cache) setting instead: with it, the indexes are kept in a shared cache of limited size and are not held by every part. Parts that already hold their index keep it until it is unloaded.

## Related pages

* [`SYSTEM UNLOAD PRIMARY KEY`](https://clickhouse.com/docs/reference/statements/system#unload-primary-key)
* [`system.parts`](https://clickhouse.com/docs/reference/system-tables/parts)
* [Debugging memory issues](https://clickhouse.com/docs/concepts/features/performance/troubleshoot/debugging-memory-issues.md)
