Sub topic 8
What VACUUM removes and what that means for history
This chapter assumes you know basic SQL, can run commands in a Databricks notebook or the SQL editor, and understand that a Delta table is a set of Parquet data files plus a transaction log. It also assumes you have read the earlier chapters on DESCRIBE HISTORY and VERSION AS OF.
Two kinds of history
Time travel depends on two separate things, and they expire on different schedules.
- The commit history in the
_delta_logfolder. It records which files each version contained. - The data files in the table's storage location. A version can only be read if its data files still exist.
Retention settings control the log. VACUUM controls the files. A version is queryable only when both are intact.
What happens to files when you change data
Delta Lake never edits a Parquet file in place. When an UPDATE, DELETE, or MERGE changes rows, Delta writes new files and marks the old ones as removed in the log. The old files stay in storage, and older versions still point at them. That is why VERSION AS OF 5 works.
These unreferenced files are not free. They use storage and you pay for them. VACUUM is the command that deletes them.
What VACUUM deletes and what it keeps
VACUUM removes data files that meet both conditions:
- The current table version no longer references the file.
- The file was marked removed longer ago than the retention threshold.
It keeps every file the current version needs. It also keeps recently removed files. The default threshold is 7 days, set by delta.deletedFileRetentionDuration.
VACUUM does not delete the transaction log entries. DESCRIBE HISTORY still lists old versions after a vacuum. The listing then shows versions you can no longer read, which is a common source of confusion.
Preview before you delete
Always run a dry run first. It lists the files that would be deleted and removes nothing.
VACUUM main.sales.orders RETAIN 168 HOURS DRY RUN;168 hours is 7 days, the default. If the output lists files you expected to keep, adjust the threshold before running the real command.
To run it for real, drop DRY RUN:
VACUUM main.sales.orders RETAIN 168 HOURS;You can also omit RETAIN to use the table's configured threshold.
Seeing the effect on time travel
Suppose version 5 of orders is two weeks old, and several updates have happened since. You run VACUUM with the default threshold. Files that only version 5 needed are now gone.
SELECT * FROM main.sales.orders VERSION AS OF 5;This query now fails, typically with an error that a file referenced in the transaction log cannot be found. The same applies to RESTORE TABLE ... TO VERSION AS OF 5. The log still says version 5 exists, but its files do not.
A version that no later version depends on is not affected as long as its files are still referenced by the current table. In practice, the versions you lose are the ones whose data has since been changed or deleted.
Keep the two retention settings in step
Recall the settings from the previous chapter.
ALTER TABLE main.sales.orders SET TBLPROPERTIES (
'delta.logRetentionDuration' = 'interval 30 days',
'delta.deletedFileRetentionDuration' = 'interval 7 days'
);With these defaults, the log keeps 30 days of history, but files are deletable after 7 days. After a vacuum, only the last 7 days of changes are reliably readable, even though DESCRIBE HISTORY shows 30 days.
If your goal is 30 days of working time travel, raise the file retention to match.
ALTER TABLE main.sales.orders SET TBLPROPERTIES (
'delta.deletedFileRetentionDuration' = 'interval 30 days'
);The cost is storage. Longer retention keeps more old files, especially on tables with frequent updates.
Do not shorten the threshold casually
You can run VACUUM with a threshold below 7 days, but Delta blocks it by default. The check exists for a reason. A long-running query or a streaming job may still be reading a file that was removed a few hours ago. Deleting that file can make the job fail or return wrong results.
Databricks manages this safety check, and the exact way to override it depends on your runtime. Treat an override as a last resort. For most tables, leave the 7-day default in place.
Practical guidance
- Run
VACUUMon a schedule, such as weekly, so storage does not grow without limit. - Always use
DRY RUNthe first time on a table, or after changing retention. - Set
delta.deletedFileRetentionDurationto at least as long as the history window you promise to users. - Do not assume that a version shown in
DESCRIBE HISTORYis readable.
Exercise
A table has delta.logRetentionDuration set to 30 days and delta.deletedFileRetentionDuration set to 7 days. You run VACUUM with the default threshold. A colleague asks for the table as it was 20 days ago. DESCRIBE HISTORY still lists that version. Will VERSION AS OF work, and why?
Show answer
It will probably fail. DESCRIBE HISTORY reads the transaction log, which keeps 30 days of commits, so the version is listed. But VACUUM deleted data files that were removed more than 7 days ago and are no longer used by the current version. If the 20-day-old version needed any of those files, the query cannot read them. To keep 20 days readable, set delta.deletedFileRetentionDuration to at least 20 days before files become eligible for deletion.
Exercise
What does VACUUM main.sales.orders RETAIN 168 HOURS DRY RUN do?
Show answer
It lists the files that VACUUM would delete under a 7-day threshold, without deleting anything. Use it to check the effect before running the real command.