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

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.