Sub topic 11
Cleaning up with VACUUM
This chapter assumes you can read a Delta table's DESCRIBE DETAIL output, and that you know OPTIMIZE rewrites small files into larger ones. It covers what VACUUM deletes, why OPTIMIZE makes it necessary, and the errors people hit when they shorten the retention period.
Why OPTIMIZE leaves files behind
OPTIMIZE never edits a Parquet file in place. It writes new, larger files and records in the transaction log that the old files are removed from the current table version. The old files stay in storage. They are no longer part of the latest version, but they still serve older versions for time travel and for queries that started before the compaction.
Storage cost therefore goes up after OPTIMIZE, not down. VACUUM is the command that deletes those unreferenced files.
Running VACUUM
VACUUM removes data files that meet both conditions: the current table version does not reference them, and they are older than the retention threshold.
VACUUM events RETAIN 168 HOURS;168 hours is 7 days, which is also the default. If you omit RETAIN, Delta uses the table's delta.deletedFileRetentionDuration property, which defaults to 7 days.
To see what would be deleted without deleting anything, use DRY RUN:
VACUUM events DRY RUN;It lists up to 1000 files that VACUUM would remove. Run it first on any table you have not vacuumed before.
What retention protects
The retention period is a safety window. Deleting a file removes it for good, and two things depend on that file staying around:
- Time travel. Queries such as
SELECT * FROM events VERSION AS OF 12need the files that version used. After VACUUM removes them, older versions can fail to read. - Long-running readers. A query or streaming job that started before the compaction may still be reading the old files. If VACUUM deletes them mid-query, the job fails with a file-not-found error.
Choose a retention period longer than your longest-running job and longer than the time travel window you need. Seven days is a reasonable starting point for most tables.
Shortening retention
Databricks blocks RETAIN values below the table's retention threshold, because they risk the failures above. The command returns an error instead of running. Lowering the table property is the supported way to change the threshold:
ALTER TABLE events SET TBLPROPERTIES (
'delta.deletedFileRetentionDuration' = 'interval 14 days'
);Raising it keeps more history, and costs more storage. Lowering it saves storage and shrinks the time travel window. Only lower it if no reader or job needs the older files.
Running it from Python
from delta.tables import DeltaTable
dt = DeltaTable.forName(spark, "events")
dt.vacuum(168)The argument is the retention in hours.
Where VACUUM fits in a maintenance routine
A typical order is: OPTIMIZE to compact, then VACUUM to reclaim storage. You can run VACUUM less often than OPTIMIZE, such as weekly, because it only deletes files past the retention period. Running it right after OPTIMIZE frees nothing, since the files just replaced are still inside the window.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Error saying the retention interval is shorter than the default | `RETAIN` is below the table's retention threshold | Use a longer value, or change `delta.deletedFileRetentionDuration` deliberately |
| Time travel query fails on an old version | VACUUM removed that version's files | Keep a longer retention period for tables that need history |
| A long query fails with a missing file | VACUUM ran while the query was still reading old files | Set retention above your longest job duration |
| Storage did not shrink after OPTIMIZE | Old files are still inside the retention window | Wait for the window to pass, then run VACUUM |
Exercise
You ran OPTIMIZE on a table this morning and then ran VACUUM with the default retention. Storage usage did not drop. Why, and what would make it drop?
Show answer
The files OPTIMIZE replaced are newer than the 7-day retention period, so VACUUM does not delete them. Storage drops once those files are older than the retention threshold and VACUUM runs again, or if you deliberately lower the table's retention property, accepting the loss of time travel and the risk to long-running readers.