All posts

Apache Iceberg table maintenance: compaction, expiry, and the space you did not reclaim

Compaction rewrites small files into large ones and reclaims nothing. The old files stay on disk, still referenced, until a separate expiry job removes them. Here is what each Iceberg maintenance procedure does to a table, measured file by file, and what to schedule.

10 min read Iceberg

TL;DR

  • Compaction does not free space. After rewriting 16 Iceberg data files into 1, the table reported 1 data file and the directory still held 17 Parquet files. Only expire_snapshots deleted the other 16.
  • Every maintenance operation is itself a commit. Compacting produced a new snapshot, and rewriting manifests produced another, so a table can gain history faster than it loses it.
  • Iceberg runs none of these operations for you. Compaction, manifest rewriting, expiry and orphan removal are four separate jobs that each fix a different problem.
  • Old metadata.json files pile up as well: eight commits left 9 of them with the defaults and 4 with cleanup switched on.
  • For the Hudi equivalents, see Apache Hudi table maintenance.

Why does an Iceberg table need maintenance at all?

An Iceberg table is append-structured. A write does not modify existing files; it adds new ones and records, in metadata, which files now constitute the table. That is what makes snapshot isolation and time travel possible, and it has a direct consequence: the files a table no longer uses do not go anywhere.

Two costs accumulate from this. Writes that arrive frequently produce many small files, and a query that must open a thousand files to read what would fit in ten spends its time on open and close rather than on data. Separately, every version of every rewritten file stays on disk, referenced by older snapshots, long after the current version of the table stopped needing it.

Maintenance is the set of operations that repair both. The distinction that matters is that they are different operations and doing the first does not do the second.

Everything below was run and measured: Iceberg 1.11.0 on Spark 4.1.3 for the small-file and expiry runs, and Spark 3.5.9 for the orphan-file and metadata-file runs, each writing to a local warehouse.

What Iceberg keeps

Catalog
   |
Table metadata (vN.metadata.json)
   |
Snapshot
   |
Manifest list (avro)
   |
Manifest (avro)
   |
Data files (parquet)

A snapshot points at a manifest list, which points at manifests, which list data files. Maintenance operations rewrite one or more of those levels, and each one leaves the previous version in place until expiry says it can go.

Which procedure fixes which symptom

Before the mechanics, the lookup table. These procedures are not interchangeable, and picking the wrong one leaves the problem exactly where it was.

Symptom Procedure
Tasks slow; many small data files rewrite_data_files
Reads slow on a CDC table; delete files growing rewrite_data_files, then rewrite_position_delete_files
Slow before any task starts rewrite_manifests
Storage grows while row count is flat expire_snapshots
Files on storage that no snapshot references remove_orphan_files
Many old metadata.json files metadata cleanup properties

The diagnostic that separates the first three: where the time goes. Planning time, before any task is scheduled, is a metadata problem: too many manifests. Task time is a data problem: too many files, or too many deletes to apply. Both show up identically in a dashboard tracking query duration.

The small-file problem, measured

Eight appends to an Iceberg table, the shape produced by any frequent writer:

CREATE TABLE local.db.events (id BIGINT, ts TIMESTAMP, region STRING, amount DOUBLE)
USING iceberg

After eight appends of 2,000 rows each, the table’s own metadata tables report:

datafiles=16   snapshots=8   manifests=8       (16,000 rows)

Sixteen data files for 16,000 rows, because two tasks wrote per append. Their sizes are the problem stated numerically:

00000-0-081691b0-...  rows=1000  bytes=10437
00000-10-0c64faa9-... rows=1000  bytes=9934
00000-12-b59f7d45-... rows=1000  bytes=9927

Ten kilobyte files. On object storage each of those is a separate GET with its own latency, and the Parquet footer of each has to be read before any data is.

Compaction rewrites them

CALL local.system.rewrite_data_files(
  table => 'db.events',
  options => map('min-input-files','2'))
rewritten_data_files_count = 16
added_data_files_count     = 1
rewritten_bytes_count      = 159392

Sixteen files became one, and the table now reports datafiles=1. The query side of the problem is solved.

Compacting only what needs it

Rewriting a whole table nightly is rarely the right shape. rewrite_data_files accepts a filter, so a job can touch only the recent partitions that actually accumulated files:

CALL local.system.rewrite_data_files(
  table   => 'db.events',
  where   => 'ts >= current_date() - INTERVAL 7 DAYS',
  options => map('min-input-files','5','target-file-size-bytes','536870912'));

It can also sort while rewriting, which is what makes column-bounds pruning work at read time:

ALTER TABLE local.db.events WRITE ORDERED BY region, ts;

Unsorted files have wide, overlapping min/max values, so the per-file statistics in the manifests eliminate nothing. After a sorting compaction each file holds a narrow slice of the sort column and filters on it start skipping files. Choose the sort column to match the filters your queries actually use. Sorting by a column nobody filters on costs a full rewrite and prunes nothing.

But the space is not reclaimed

Counting actual files in the warehouse directory immediately afterwards:

parquet files on disk = 17
metadata.json files   = 11
avro (manifests+lists)= 28

Seventeen. The sixteen originals are still there, because snapshot 8 still references them and time travel to that snapshot must still work. The table logically has one data file; the storage bill is for seventeen.

Notice too that the snapshot count went from 8 to 9. Compaction is a write, so it commits a new snapshot. Running rewrite_manifests afterwards took manifests from 9 to 1 and pushed the snapshot count to 10. Maintenance operations generate history as they remove it, which is why they cannot substitute for expiry.

Expiry is what deletes

CALL local.system.expire_snapshots(
  table => 'db.events',
  older_than => TIMESTAMP '2030-01-01 00:00:00',
  retain_last => 1)
deleted_data_files_count     = 16
deleted_manifest_files_count = 17
deleted_manifest_lists_count = 9

And on disk afterwards: 1 Parquet file, with all 16,000 rows still readable. The old data files, the manifests that listed them and the manifest lists that pointed at those are gone together, because nothing references them any more.

This is the operation people forget. A table compacted nightly and never expired grows forever while reporting a healthy file count.

The other two operations

rewrite_manifests addresses metadata rather than data. Manifests accumulate one per write, and a planner that has to open hundreds of them to decide which data files to read is slow before it has read any data. The measured run took 9 manifests to 1.

remove_orphan_files deletes files in the table’s directory that no snapshot references at all, the residue of failed or killed writes. It is distinct from expiry because those files were never committed, so no snapshot ever pointed at them and expiry will never consider them. It works by listing the directory and comparing against metadata, so it should be run with a conservative age threshold: a file written by a currently running job looks exactly like an orphan.

An orphan is easy to simulate. A data file was copied into a table’s data directory without a commit, and its modification time set to three days ago. A dry run lists it, and the real run deletes it:

CALL demo.system.remove_orphan_files(table => 'c2.t',
  older_than => TIMESTAMP '2026-11-30 00:00:00', dry_run => true);
orphan_file_location
<warehouse>/c2/t/data/orphan-from-failed-job.parquet

After the real run, two data files remained: the two that the table’s snapshots reference. The procedure also protects you from a short age limit. With older_than less than 24 hours ago, it refuses to run:

IllegalArgumentException: Cannot remove orphan files with an interval less than 24 hours.
Executing this procedure with a short interval may corrupt the table if other operations
are happening at the same time.

Old metadata files pile up too

Every commit writes a new metadata.json, and the old ones are not deleted by default. Two tables with eight commits each, one with the defaults and one with write.metadata.delete-after-commit.enabled and write.metadata.previous-versions-max set to 3:

Table metadata.json files after 8 inserts
Defaults 9
Cleanup on, 3 previous versions 4

The first table keeps one file for the creation and one for each insert. The second keeps the current file and the three before it. These files are small next to data, but each lists every retained snapshot, so on a table that commits often and never expires, every commit rewrites an ever larger file.

What to schedule, and how often

Compaction: as often as writes create small files. A streaming writer committing every minute needs this hourly; a nightly batch load may need it weekly. The signal is average file size, not a calendar.

Expiry: as often as compaction, and never less. This is the one that controls storage cost, and its retention window is a real recovery capability. Retaining seven days of snapshots means seven days in which a bad write can be undone, and the storage for every file those snapshots reference.

Manifest rewriting: when planning gets slow. Watch how long query planning takes before the first task starts.

Orphan removal: rarely, with a conservative age threshold. Days, not hours. Deleting a file a running job is about to commit is a data-loss bug you caused with a cleanup job.

Sequence them. Compact first, then expire. Expiring before compacting leaves the small files as the current state, and the compaction that follows immediately creates a fresh generation of unreferenced files.

A shape that works in practice:

hourly   rewrite_data_files on hot tables (with a where clause)
         rewrite_position_delete_files on CDC tables
daily    expire_snapshots (retention = your recovery window)
weekly   rewrite_manifests where planning is slow
monthly  remove_orphan_files with a conservative age threshold

Maintenance jobs are writers. They commit, they can conflict with ingestion, and they deserve their own compute rather than competing with the pipeline they are meant to help.

Symptoms and causes

Storage grows while the table’s row count is flat. Expiry is not running. Compaction alone produces exactly this.

Queries slow down over weeks with no data growth. Small files accumulating, or manifests accumulating. Check average data file size, then manifest count.

Query planning takes longer than query execution. Metadata, not data. Rewrite manifests.

Time travel to a recent snapshot fails. Expiry retention is shorter than you think it is. The retention window and the recovery window are the same number.

Key takeaways

An Iceberg table has two independent problems: too many files, and too many versions of files. Compaction fixes the first. Expiry fixes the second. Iceberg does neither until you run it.

When storage grows and nothing explains it, you are compacting without expiring. When queries slow and the data has not grown, you are writing without compacting.

References

Trademarks

Apache Iceberg, Apache Hudi, Apache Spark, Apache Parquet, Apache and the Apache feather logo are either registered trademarks or trademarks of The Apache Software Foundation in the United States and other countries.

Found this useful?

These posts and tools are free. If one saved you an afternoon, you can buy me a coffee.

Buy me a coffee

Comments

Questions, corrections and reviews are welcome. Sign in with GitHub to post; comments are stored in this site's public GitHub Discussions.