Compaction and Maintenance
The small-file problem
Both formats let you commit as often as you like — that's the whole point. But every commit that adds files, however few and however small, adds one more manifest entry in Iceberg or one more add action in Delta. A streaming job committing a few hundred KB every 30 seconds is doing nothing wrong, and will still, left alone, certainly end up with a table made of tens of thousands of tiny files.
| Cost | What actually gets worse |
|---|---|
| Query planning | More manifest or add entries to read and filter through (see Iceberg's metadata tree, Delta's transaction log) |
| Query execution | Opening a 1 MB Parquet file costs about the same fixed overhead — read the footer, set up decompression — as opening a 500 MB one. With tiny files, that overhead becomes most of the work |
| Storage and listing | More objects for the store to track, and you pay per request on S3 and per listing on GCS |
Both ecosystems aim for roughly 512 MB files by default (Iceberg's write.target-file-size-bytes, Delta's OPTIMIZE default). A table whose typical file is a few MB instead is the single most common reason behind "this table got slow" tickets.
Compaction
| Iceberg | Delta Lake | |
|---|---|---|
| Command | CALL system.rewrite_data_files('db.events') | OPTIMIZE events |
| What it does | Reads many small files and writes them back as fewer files at the target size, committed as a normal new version. Old files out, new files in, all in one commit | |
| How files are grouped | binpack by default, or sort/zorder to also re-cluster the rows while rewriting | Bin-packing by default; adding ZORDER BY re-clusters on several columns in the same rewrite (see Partitioning and data skipping) |
| Cost | A real rewrite. Compute and I/O scale with how much data it touches, and it can collide with concurrent writers on the same files (see Concurrency control) |
Because compaction reads and rewrites real data, you normally schedule it — nightly, or once the small-file count crosses a threshold — instead of running it constantly. Running it aggressively to keep files always tidy burns compute for very little gain and causes more collisions.
Bin packing: how compaction actually picks what to rewrite
Bin packing isn't table-format jargon — it's the classic problem. You have items of varying size and bins of fixed capacity, and you want to use as few bins as possible. It's NP-hard, so no production system solves it exactly; they all run a greedy pass and accept a slightly worse answer. Compaction is a direct instance of it:
| Bin-packing term | What it is during compaction |
|---|---|
| Item | One existing data file. Its weight is its size in bytes |
| Bin capacity | The target file size — Iceberg's target-file-size-bytes (defaults to the table's write.target-file-size-bytes, 512 MB) |
| A packed bin | One rewrite task, producing one new file at roughly the target size |
| Objective | Fewest output files, without overshooting the target enough to hurt parallelism |
The part that matters operationally is that packing happens per partition and skips files that are already fine. A file belongs to exactly one partition (see Partitioning and data skipping), and merging across partitions would destroy the pruning that partitioning exists for — so the packer never does it.
What Iceberg's binpack strategy does, step by step
| # | Step | Knob (and default) |
|---|---|---|
| 1 | Pick candidates. Only files outside the acceptable size window are eligible — too small, or too large. Files already close to target are never even read, which is what makes a nightly re-run cheap on a mostly-tidy table | min-file-size-bytes = 75% of target, max-file-size-bytes = 180% of target |
| 2 | Pull in delete-heavy files too. A right-sized file with a pile of delete files attached is still worth rewriting, to fold the deletes back in (see below) | delete-file-threshold = unlimited, i.e. off by default |
| 3 | Group by partition, then bail on any partition with too few candidates — rewriting 2 small files into 1 isn't worth a commit and a conflict window (see Concurrency control) | min-input-files = 5; rewrite-all = false overrides both this and step 1 |
| 4 | Greedily pack. Walk the candidate list, dropping each file into one of the last few still-open bins it fits in; open a new bin when none does. The bounded lookback is what keeps planning cheap on a table with 100k candidate files | lookback window, plus max-file-group-size-bytes = 100 GB so one task can't blow up an executor |
| 5 | Rewrite bins in parallel, then commit — one commit at the end, or incremental ones so a failure at hour six doesn't throw away the first five | max-concurrent-file-group-rewrites = 5; partial-progress.enabled, partial-progress.max-commits = 10 |
Worked example — one partition, one nightly run
Target 512 MB, so the skip window is 384 MB – 921 MB. Partition day=2026-08-21 holds 62 files: sixty 12 MB files from a streaming job, one 500 MB file from yesterday's compaction, and one 1.2 GB file from a bulk backfill.
What that commit actually writes into the metadata tree — which manifests get rewritten, which are merely pointed at again — is traced file by file in the Iceberg's metadata tree part.
Note that the oversized file is also a candidate — bin packing splits as well as merges. A 1.2 GB file is worse than three 512 MB ones because it's a unit of parallelism: one task has to read all of it.
Delta's version, and where it stops being bin packing
Iceberg rewrite_data_files | Delta OPTIMIZE | |
|---|---|---|
| Bin capacity | target-file-size-bytes, 512 MB | spark.databricks.delta.optimize.maxFileSize, 1 GB in OSS Delta; Databricks auto-tunes it by table size |
| Skip rule | Files inside [min-file-size-bytes, max-file-size-bytes] | Files at or above optimize.minFileSize |
| Parallelism | Bins rewritten as concurrent file groups | Each bin is a job, run across optimize.maxThreads |
| With clustering | sort / zorder — no longer bin packing | ZORDER BY — no longer bin packing |
That last row is the important distinction. Bin packing never reorders rows — it concatenates whole files, so a row's neighbours after compaction are the rows it already sat next to. That's exactly why it's cheap: no shuffle, no global sort, and each bin is independent so the work scales out perfectly.
Asking for sort or ZORDER gives up all three. Rows have to be redistributed across the whole partition, which means a shuffle, which means the bins are no longer independent and can't be planned by size alone. You pay for it once and get better min/max pruning on the clustered columns forever after (see Partitioning and data skipping). The rule of thumb: bin-pack when your problem is file count, sort or Z-order when your problem is files that can't be skipped.
Row-level deletes: why they exist and what they cost
Before Iceberg v2 and Delta's deletion vectors, the only way to change or delete rows was copy-on-write: find every file holding an affected row, rewrite each of those files completely, and commit the replacements. Deleting one row out of a 500 MB file meant rewriting all 500 MB.
Merge-on-read avoids that. Write a small delete file that records which rows are gone, and let each read apply those deletes to the data file as it goes.
| Copy-on-write (CoW) | Merge-on-read (MoR) | |
|---|---|---|
| Write cost | High. Every affected file gets rewritten in full, even to change one row | Low. A few KB of delete file, and the data file isn't touched |
| Write latency | Scales with the size of every file you touched | More or less constant, whatever the data file size |
| Read cost | Low. Open the files and read, nothing to apply | Higher. Every read applies each file's deletes, and it gets worse as deletes pile up |
| Best fit | Read-heavy tables with occasional, small updates — nightly dimension updates, the odd GDPR delete | Write-heavy tables with constant row-level changes — exactly what a CDC pipeline looks like |
Iceberg: positional and equality deletes
Data file: events-00042.parquet (500 MB, 2,000,000 rows)
Row 1,500,000 needs deleting. Instead of rewriting 500MB:
Positional delete file (a few KB):
┌──────────────────────────┬───────────┐
│ file_path │ position │
├──────────────────────────┼───────────┤
│ .../events-00042.parquet │ 1500000 │
└──────────────────────────┴───────────┘
Read path: open events-00042.parquet, open the positional delete file,
skip row 1500000 while decoding. The 500MB file is never rewritten.UPDATE, concretely: a delete plus a tiny new file
A row-level UPDATE isn't a native operation — it's always delete the old row, insert the new one. In merge-on-read mode that means two small writes, and the original file is never touched:
Data file: events-00042.parquet (100 rows)
UPDATE events SET status='shipped' WHERE id IN (row 12, row 47) -- 2 of 100 rows
1. Positional delete file — marks the 2 OLD rows gone:
{file_path: events-00042.parquet, position: 12}
{file_path: events-00042.parquet, position: 47}
2. New data file, events-00099.parquet (2 rows) — holds ONLY the new
values for those 2 rows. The other 98 rows are never read or copied.
Read path: open events-00042.parquet, drop positions 12 and 47 via the
delete file (98 rows survive) → union with events-00099.parquet (2 rows)
→ 100 rows total, the 2 changed ones now showing the new values.There's no lookup or index involved in finding "the right record" — the delete file names the exact old position (or an equality predicate) tied to a specific data file, and the reader just subtracts and unions per file. This is merge-on-read behavior specifically; under
write.update.mode = copy-on-write, the whole 100-row file gets rewritten with the 2 values changed and no delete file is written at all — the "new file" would contain all 100 rows, not 2.
Equality deletes are the other option. Instead of "skip this exact position," they say "skip any row where user_id = 42." That's useful when the writer doesn't know the positions — a streaming CDC job deleting by key across files it never read. They cost more to apply at read time, since every row has to be checked against the condition, but far less to write when you don't know positions.
From the Iceberg's metadata tree part: sequence numbers keep this safe when commits overlap. A positional delete file only applies to data files with an equal or lower sequence number (an equality delete, strictly lower), so a file added by a later commit is correctly left alone by an earlier delete that couldn't have known about it.
Delta Lake: deletion vectors
Same idea, packaged differently. Instead of a delete file with one row per deleted row, Delta writes one bitmap per data file: if bit i is set, row number i is deleted. It stores nothing about the row's contents, only its position — same as Iceberg.
Data file: part-00042.parquet (500 MB, 2,000,000 rows)
Deletion vector (roaring-bitmap-encoded side file):
bit 1500000 = 1 (all other 1,999,999 bits = 0)
The `add` action for that file is rewritten to point at the vector —
itself a small metadata-only commit, not a data rewrite:
{"add":{"path":"part-00042.parquet","size":524288000,
"deletionVector":{"storageType":"u","pathOrInlineDv":"...","cardinality":1}}}Why a bitmap instead of a list of positions? A roaring bitmap stays a few KB even as more rows in the same file get deleted, because it compresses long runs of bits. So one growing side file per data file works out cheaper than the many small delete files Iceberg's positional approach can accumulate. A reader has to understand the deletionVectors table feature (see Delta's transaction log) to hide those rows correctly — which is exactly why an unknown table feature has to make the read fail outright rather than quietly return the wrong rows.
Iceberg format v3 (see Iceberg's metadata tree) adopts the same design: positional deletes become one roaring bitmap per data file, stored in a Puffin file. So the "many small delete files" contrast above is a v2-era difference, not a permanent one — on a v3 table both formats carry deletes the same way.
The cost of merge-on-read
Merge-on-read makes deletes cheap to write and moves the cost onto every read after that. A data file with a pile of small delete files against it pays that cost again on every single scan, until compaction folds the deletes into a rewritten file.
That's why delete files and deletion vectors need their own periodic cleanup: rewrite_data_files in Iceberg, OPTIMIZE or REORG TABLE … APPLY (PURGE) in Delta. Both fold the accumulated deletes into fresh files with no deletes attached, resetting the table to the cheap-read state instead of letting read cost climb forever.
Cleaning up: expiring snapshots and VACUUM
Data files that only old snapshots point at are not deleted automatically. Both formats keep them on purpose, because time travel (see Schema evolution and time travel) and any reader still working from an older snapshot need them to exist.
| Iceberg | Delta Lake | |
|---|---|---|
| Command | CALL system.expire_snapshots('db.events', older_than => …) | VACUUM events RETAIN 168 HOURS (default: 7 days) |
| What it does | Drops snapshot entries older than the cutoff from metadata.json, then deletes any data or manifest files that no remaining snapshot points at | Deletes files marked removed longer ago than the retention window, as long as no commit inside that window still references them |
| Files nothing points at | CALL system.remove_orphan_files(…) cleans up files in storage that no metadata mentions — usually left behind by a job that wrote data and then crashed before committing | VACUUM also removes untracked files under the table directory, by the same reasoning |
Worked example: what expire_snapshots actually deletes
S1: write d1, d2 → manifest M1 [ADDED d1, ADDED d2] manifest-list ML1 → [M1]
S2: append d3 → manifest M2 [ADDED d3] manifest-list ML2 → [M1, M2]
S3: compact d1+d2+d3→d123 → manifest M3 [ADDED d123, DELETED d1, manifest-list ML3 → [M3]
DELETED d2, DELETED d3] (M1, M2 dropped — their files
are gone from the current view)
expire_snapshots(older_than=now, retain_last=1) -- keeps S3, expires S1 and S2
Step 1 — metadata.json drops the S1 and S2 snapshot entries.
Step 2 — sweep: keep only what's reachable from the SURVIVING snapshots.
Reachable from S3: ML3 → M3 → d123.parquet
Unreachable, deleted from storage: ML1, ML2, M1, M2, d1.parquet, d2.parquet, d3.parquetThis is reachability, not "delete anything tied to an old snapshot." If
retain_last=2had kept S2 alive instead,d3.parquetwould still be reachable viaS2 → ML2 → M2and would survive — even though d1/d2 (only reachable via the now-gone S1) would still be deleted. A tag or branch pinned to an old snapshot keeps its files alive indefinitely, no matter how long ago they'd otherwise have expired.
Concurrent writers vs. cleanup: why nothing races
Obvious question from the worked example above: what if a writer is appending, or another rewrite_data_files is running, at the same time as expire_snapshots? There's no special case built for this — cleanup is just another commit, bound by the identical the Concurrency control part rule as every writer: you can only build on top of current-snapshot-id, and you only become the new current-snapshot-id by winning a compare-and-swap. Two consequences fall out of that alone:
- The snapshot a writer is building on can never be the one that's expiring.
older_than/retain_lastonly ever drop snapshots older than whatever's current (the Iceberg's metadata tree part's rule: the current snapshot itself can never be expired) — so a writer mid-flight is always pinned to a snapshot that's outside the expiry job's reach, no matter where the cutoff falls. - Whichever commit's CAS lands first defines reality for the other. There's no in-between state where two commits are both half-live — one becomes current, the other has to react to it.
| Who lands first | What happens next |
|---|---|
| Writer's commit lands first | Its new snapshot becomes current. When expire_snapshots runs Step 2 of the sweep, it computes keeping against whatever's actually current at that moment — which now includes the writer's snapshot. Any manifest that snapshot still references lands in keeping, never in the delete set. |
expire_snapshots's commit lands first | The writer's CAS fails — its expected current-snapshot-id is stale. Per the Concurrency control part it re-reads the new metadata, confirms this isn't a real conflict (dropping old snapshots[] entries never touches live data files, so it's the same "no conflict, just retry" case as two concurrent appends), and rebuilds its manifest list on top of the post-expiry metadata. It never resubmits the old reference — it recomputes fresh, so there's nothing left to point at what cleanup just dropped. |
Compaction races the identical way — it's just a commit that drops old manifest entries instead of copying them forward (the M1, M2 → M3 step above). Same pin-to-current, same CAS, same rebuild-and-retry on conflict. And compaction landing doesn't free anything by itself either: right after S3 committed,
d1/d2/d3stayed on disk, still reachable via S1/S2 sitting insnapshots[]—expire_snapshotsis still the only thing that turns "unreferenced by current" into "actually deleted."
Net effect: a file is deleted only if it's unreachable from every snapshot present in
metadata.jsonat the exact moment the sweep runs — and that snapshot set is always the fully-resolved outcome of whatever CAS race just happened, never a stale view frozen before it. Nothing can sneak in a fresh reference to a manifest that's already gone, and nothing gets deleted out from under a commit that already won its race.
The Invariants
- Guarantees: Time travel, and every snapshot inside the retention window, stay fully readable. Expiry and vacuum never delete a file that a still-valid snapshot needs.
- Does not guarantee: Neither runs any of this for you on a sensible default schedule. Compaction, folding deletes back in, and expiry are your jobs to run, not background behavior. A table with no maintenance scheduled just keeps getting worse.