Databricks DE ยท Topic 3.3

VACUUM & Time-Travel UNDO

VACUUM permanently deletes old, unreferenced data files past the retention window (default 7 days) โ€” reclaiming storage, but ending time travel beyond that point. This deep dive walks the lifecycle and the code.

7-day defaultRETAIN n HOURSDRY RUNLITE / FULLsafety check

The one-liner

VACUUM removes data files no longer referenced and older than the retention threshold (default 7 days). It reclaims storage and finalizes deletes โ€” but versions older than retention are no longer time-travelable afterward.
๐Ÿ—‘๏ธ Analogy: VACUUM is emptying the recycle bin. A DELETE just moves old files to "trash" (still time-travelable); VACUUM empties the bin for anything older than retention โ€” space back, but no more undo for those.

Retention vs time travel โ€” drag the threshold

โ–ธ files older than retention get purged on VACUUM

7

1 ยท The delete โ†’ vacuum lifecycle

Row removal happens in two stages โ€” the model interviewers want: a logical tombstone (files stay, still time-travelable, no space freed), then a physical removal by VACUUM once past retention.

DELETE FROM main.sales.events WHERE event_date < '2026-01-01';  -- stage 1: logical
VACUUM main.sales.events;                                       -- stage 2: physical (past retention)

2 ยท DELETE vs VACUUM โ€” click each

VACUUM main.sales.events RETAIN 168 HOURS;   -- explicit 7-day retention
VACUUM main.sales.events DRY RUN;            -- preview what would be purged
VACUUM main.sales.events LITE;               -- log-only scan (Public Preview, DBR 16.4 LTS+)
1 ยท DELETE / UPDATE / MERGE / OPTIMIZEโ–ธ
Marks old files "unused" (tombstones) in the log but leaves them on disk. Data is still recoverable via time travel โ€” nothing is freed yet.
2 ยท VACUUM โ€ฆ DRY RUNโ–ธ
Previews exactly which files would be deleted, without removing anything. Always do this first on important tables.
3 ยท VACUUMโ–ธ
Permanently deletes unused files older than retention. Storage reclaimed; time travel to those versions is now gone. Below-threshold VACUUM is blocked by a safety check.

PySpark โ€” DeltaTable API

from delta.tables import DeltaTable
dt = DeltaTable.forName(spark, "main.sales.events")
dt.vacuum()        # default retention
dt.vacuum(168)     # retain 168 hours

3 ยท The retention safety check (interview trap)

Files are eligible only past delta.deletedFileRetentionDuration (default 7 days). Databricks blocks a below-threshold VACUUM โ€” a long-running query started before the VACUUM may still need files you'd delete.

โš ๏ธ Only if you understand the risk. This can break time travel AND corrupt concurrent readers/writers. Keep retention โ‰ฅ your longest job runtime.
SET spark.databricks.delta.retentionDurationCheck.enabled = false;
VACUUM main.sales.events RETAIN 0 HOURS;   -- dangerous; rarely correct

Tune retention (longer rollback window, more storage)

ALTER TABLE main.sales.events
  SET TBLPROPERTIES ('delta.deletedFileRetentionDuration' = 'interval 30 days');

4 ยท Automation & the compliance angle

Why it matters

๐Ÿ’ฝ Real-world use case: a heavily-updated silver table balloons in storage. A scheduled VACUUM (7-day retention) clears stale files nightly โ€” keeping a week of rollback while controlling cost.

Logical vs physical removal

DELETE / MERGE / OPTIMIZEVACUUM
Effecttombstones old files (logical)physically deletes them
Space freedโŒ not yetโœ… (files past retention)
Time travel to old versionโœ… still worksโŒ gone after VACUUM
Automatic?n/aonly via Predictive Optimization (managed)

Uses, edge cases & limitations

Common gotchas