Sub topic 7
Retention settings that control how far back you can go
This chapter assumes you can run SQL in a Databricks notebook or the SQL editor. It also assumes you know what a Delta table is and have read the earlier chapters on the transaction log, DESCRIBE HISTORY, and VERSION AS OF. Here you will learn which two settings decide how far back time travel reaches, and how to change them safely.
Two kinds of history
Time travel needs two things to be present for a given version:
- The commit entry in the transaction log (the JSON file in
_delta_logthat describes what changed). - The data files that version points to (the Parquet files).
Delta Lake keeps these on separate schedules. Each schedule has its own table property.
| Property | What it controls | Default |
|---|---|---|
| `delta.logRetentionDuration` | How long commit history is kept in the transaction log | 30 days |
| `delta.deletedFileRetentionDuration` | How long a data file that is no longer part of the current table must be kept before `VACUUM` may delete it | 7 days |
A version is only readable while both are still available. The shorter of the two effective windows usually decides how far back you can go.
Reading the current settings
Table properties are stored with the table. To see them, run:
SHOW TBLPROPERTIES main.sales.orders;If neither property appears in the output, the table uses the defaults in the table above.
Changing the settings
Use ALTER TABLE ... SET TBLPROPERTIES. The values are written as an interval string. This example sets the defaults explicitly:
ALTER TABLE main.sales.orders SET TBLPROPERTIES (
'delta.logRetentionDuration' = 'interval 30 days',
'delta.deletedFileRetentionDuration' = 'interval 7 days'
);To keep more history, raise both values together. For example, a table that auditors query 60 days back needs a log retention of at least 60 days and a deleted-file retention of at least 60 days:
ALTER TABLE main.sales.orders SET TBLPROPERTIES (
'delta.logRetentionDuration' = 'interval 60 days',
'delta.deletedFileRetentionDuration' = 'interval 60 days'
);Raising only the log retention is a common mistake. The log will still list old versions, but their data files can be deleted after 7 days, so queries on those versions fail.
How the windows are applied
The two settings are not enforced at the same moment.
- Log entries are cleaned up automatically. Delta removes expired commit entries when it writes checkpoints, which happens as part of normal writes. You do not run a command for this.
- Data files are not deleted automatically by the retention property. The property only says how old a removed file must be before
VACUUMis allowed to delete it. Files stay on storage until someone (or a scheduled job, or predictive optimization if enabled for the table) runsVACUUM.
So the real limit depends on how often VACUUM runs. If VACUUM has not run for a month, data for versions older than 7 days may still exist, and time travel may still work. Do not rely on this. Treat the configured windows as the guarantee and anything beyond them as luck.
Checking the effect before you delete
VACUUM has a dry-run mode. 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, matching the default. Run this before shortening retention or lowering the RETAIN value, so you can see how much history you would lose.
Trade-offs and common errors
Longer retention costs storage, because every file removed from the current table is still kept for the whole window. A table that is rewritten often, for example by daily MERGE or UPDATE jobs on large partitions, can hold several copies of its data at once. Choose the shortest window that meets your audit or recovery need.
Common problems:
- Error reading an old version. If a query fails because files are missing, the version is past the deleted-file window and
VACUUMhas already removed its data. The history may still show the version inDESCRIBE HISTORY, but it can no longer be read. - Version not found. If the commit has aged out of the log, the version number is no longer available at all.
- Shortening retention too far. Setting
RETAINbelow the table's configured deleted-file retention makes Databricks stop the command with a safety check. The check exists because a long-running query or a concurrent reader may still need those files. Do not disable it just to reclaim space. - Changes only apply going forward. Changing a property does not bring back files that
VACUUMhas already deleted.
Exercise
A table has delta.logRetentionDuration set to 90 days and delta.deletedFileRetentionDuration left at the default. VACUUM runs nightly. A user tries VERSION AS OF on a version committed 20 days ago and gets a missing-file error, even though DESCRIBE HISTORY still lists it. Why, and what would you change to support 90 days of time travel?
Show answer
The log keeps the commit for 90 days, so the version appears in history. But the deleted-file retention is the default 7 days, and the nightly VACUUM has already deleted the data files that version needs. To support 90 days of time travel, set delta.deletedFileRetentionDuration to interval 90 days as well. This only protects versions whose files have not yet been vacuumed, so it will not recover the 20-day-old version that has already lost its files.