Compaction and Cleanup: The Jobs Nobody Runs for You
run ( ctx , spark , "CALL hms.system.rewrite_data_files(table => 'db.orders')" )
run ( ctx , spark , "CALL hms.system.rewrite_manifests('db.orders')" )
run ( ctx , spark , `
CALL hms.system.expire_snapshots(
table => 'db.orders',
older_than => TIMESTAMP '2026-08-16 09:41:15',
retain_last => 3
)` )
run ( ctx , spark , "CALL hms.system.remove_orphan_files(table => 'db.orders')" )
rewrite_data_files resolves every position-delete from the Lab: upserts with MERGE INTO part by physically dropping the superseded rows. That part is unambiguous, and you can watch it happen:
before, then after — SELECT content, count(*) FROM hms.db.orders.files GROUP BY content
before: after:
+-------+---+ +-------+---+
| content| n | | content| n |
+-------+---+ +-------+---+
| 0 | 7 | | 0 | 8 |
| 1 | 1 | <- delete +-------+---+
+-------+---+ # content=1 is gone: the delete was applied and dropped
Look at the data-file count again: it went up, 7 → 8.
The word "compaction" primes you to expect fewer, larger files, and on this table that did not happen — the run reported rewritten_data_files_count=6, added_data_files_count=7. Nothing is broken. rewrite_data_files works within a partition , and this table has one or two tiny files per day-partition, so there is almost nothing to merge; what it actually did was rewrite files to apply the delete. The default bin-pack strategy also only bothers with a partition once it exceeds min-input-files (5 by default), which no partition here does.
The lesson is the one that matters operationally: compaction's win comes from many small files inside one partition , which is what a streaming or micro-batch writer produces and what a seven-row lab does not. Measure rewritten_data_files_count against added_data_files_count on your own tables rather than assuming the number fell.
expire_snapshots is the step that actually reclaims bytes for anything that rewrite made unreachable — on this table it deleted 4 manifest files and 4 manifest lists. Run it without a preceding rewrite on a purely append-only history and — per the main guide's self-test Q6 — you'll reclaim almost nothing, because nothing was ever made unreachable.
remove_orphan_files refuses to run on recent files, on purpose.
Call it with an explicit recent cutoff — older_than => current_timestamp(), the obvious thing to try when you want to see it do something — and Iceberg refuses outright:Cannot remove orphan files with an interval less than 24 hours.
That is a deliberate guardrail, not a bug. Orphan detection works by listing the directory and subtracting every file the metadata references; a file being written right now by a concurrent commit is indistinguishable from an abandoned one, so deleting recent files can corrupt a table someone else is mid-commit on. The no-argument form used above defaults to a 3-day cutoff and is the one you want. On this lab's table it correctly finds nothing.
A five-line Go housekeeping job.
There's nothing special about wrapping these four calls in a function and putting it on a time.Ticker or a scheduled Cloud Run job — this is a completely ordinary Go maintenance daemon, it just happens to be maintaining an Iceberg table instead of a queue or a cache:
func runMaintenance(ctx context.Context, spark sql.SparkSession, table string) {
run(ctx, spark, fmt.Sprintf("CALL hms.system.rewrite_data_files(table => '%s')", table))
run(ctx, spark, fmt.Sprintf("CALL hms.system.rewrite_manifests('%s')", table))
run(ctx, spark, fmt.Sprintf(
"CALL hms.system.expire_snapshots(table => '%s', older_than => current_timestamp() - INTERVAL 7 DAYS, retain_last => 3)", table))
}