What you will be able to do
- Explain why a Delta table version stops being queryable, and which two table properties control this
- Predict how VACUUM limits how far back time travel can reach
- Use a historical version as a source in INSERT, MERGE and comparison queries
- Roll a table back with RESTORE and state its permission requirement and downstream side effects
1.How far back you can travel
A historical version is readable only while two things still exist: its entry in the transaction log, and the data files it points to. Each is removed on its own schedule, by a separate mechanism:
- Data files are deleted when VACUUM runs against the table. With predictive optimization enabled, Databricks runs VACUUM for you automatically. - Log files are removed automatically after table versions are checkpointed.
Two table properties set these thresholds. In each name, <format> is delta or iceberg.
| Table property | Controls | Default |
|---|---|---|
| delta.logRetentionDuration | How long the history for a table is kept | interval 30 days |
| delta.deletedFileRetentionDuration | The threshold VACUUM uses to remove data files no longer referenced in the current table version | interval 7 days |
The shorter of the two windows wins. Once VACUUM has removed the data files for a version, the log may still describe that version, but you can no longer read it. Newer runtimes enforce this. In Databricks Runtime 18.0 and above, a time travel query is blocked if it asks for a version older than deletedFileRetentionDuration. For Unity Catalog managed tables, this applies from Databricks Runtime 12.2.
To keep 30 days of readable history, set delta.deletedFileRetentionDuration = "interval 30 days", which matches the log default. On those same runtimes, logRetentionDuration must be greater than or equal to deletedFileRetentionDuration. The cost is storage: more old data files are kept. You can set these properties when you create the table or later with ALTER TABLE.
Checkpoint 1 of 4· Check yourself
A team needs to run time travel queries 30 days back on a Delta table that currently has default retention settings. Which change makes that possible?
The log already keeps 30 days by default. The limit is data files, which VACUUM can remove after 7 days, so the data file retention has to be raised to 30 days.
“to access 30 days of historical data, set delta.deletedFileRetentionDuration = "interval 30 days", which matches the default setting for delta.logRetentionDuration.”Source: docs.databricks.com
Checkpoint 2 of 4· Exam question
A batch job accidentally overwrote the `customers` table with a bad transformation two versions ago. The team wants the table itself reverted to that earlier state, not just a query that reads it. Which command accomplishes this?
Correct answer: A — `RESTORE TABLE customers TO VERSION AS OF 118;`, which reverts the current table to the specified version and records the restore as a new commit
- A. Correct: `RESTORE TABLE` reverts a Delta table to an earlier version or timestamp and writes that reversion as a new transaction log entry, changing the table's live state rather than just returning a read-only snapshot.
- B. Incorrect: `SELECT ... VERSION AS OF` only reads a historical snapshot for querying; it does not modify the live table, and manually re-inserting every row is not how time travel restoration works.
- C. Incorrect: there is no `current_version` table property that repoints a Delta table to an older state; table properties configure behavior like retention settings, not which version is currently active.
- D. Incorrect: `TIMESTAMP AS OF` does not accept an interval expressed in versions, and `COPY INTO` is designed for ingesting external files, not for reverting a table to its own earlier state.
2.Using an old version as a query source
A time-travelled table reference works like any other table in a FROM or USING clause. That means you can read old rows and write them into the current table, all in SQL. The documentation gives three patterns.
Recover deleted rows. If a user's rows were deleted by mistake, select those rows from yesterday's version and insert them back:
INSERT INTO my_table
SELECT * FROM my_table TIMESTAMP AS OF date_sub(current_date(), 1)
WHERE userId = 111Undo bad updates. Use yesterday's version as the source of a MERGE. Matched rows are overwritten with their earlier values: MERGE INTO my_table target USING my_table TIMESTAMP AS OF date_sub(current_date(), 1) source ON source.userId = target.userId WHEN MATCHED THEN UPDATE SET *.
Compare points in time. Subtract last week's distinct count from today's to get the number of new customers. One subquery reads my_table, and the other reads my_table TIMESTAMP AS OF date_sub(current_date(), 7).
All three use a relative timestamp expression, so the same query works on any day. None of them changes the table's history. The fixes are ordinary new writes, recorded as new versions.
Checkpoint 3 of 4· Check yourself
Some rows in my_table were updated incorrectly today. You want to set the affected rows back to yesterday's values and leave all other changes made today in place. Which approach matches the documented pattern?
Using yesterday's version as the MERGE source puts earlier values back on matching rows only. VACUUM removes old files, which would make recovery harder, not undo the change.
“USING my_table TIMESTAMP AS OF date_sub(current_date(), 1) source”Source: docs.databricks.com
Sources1
3.Rolling the whole table back with RESTORE
When the whole table needs to go back, not just some rows, use RESTORE. It accepts the same two forms of time travel argument and returns the table to an earlier state. You can restore a table that was already restored, and you can restore a cloned table.
RESTORE TABLE target_table TO VERSION AS OF <version>;
RESTORE TABLE target_table TO TIMESTAMP AS OF <timestamp>;Four facts about RESTORE are worth knowing:
- Permission: you need MODIFY on the table. A SELECT on an old version only reads, but RESTORE writes.
- Timestamp formats: when restoring by timestamp, use yyyy-MM-dd HH:mm:ss or yyyy-MM-dd.
- Retention still applies: after VACUUM or a manual delete has removed a version's data files, you can't restore to that version.
- It is a new write: RESTORE adds a new version, and its log entries have dataChange set to true. A downstream job such as a streaming read sees the restored files as new data, which can cause duplicates.
When it finishes, RESTORE returns one row of metrics, including num_restored_files and table_size_after_restore.
Checkpoint 4 of 4· Check yourself
An analyst with only SELECT on a table can run SELECT * FROM sales VERSION AS OF 5. When they try RESTORE TABLE sales TO VERSION AS OF 5, it fails. What is the most likely reason?
Reading an old version only needs read access. RESTORE changes the table, so it needs MODIFY. RESTORE supports both VERSION AS OF and TIMESTAMP AS OF.
“To restore a table, you must have MODIFY permission for the table.”Source: docs.databricks.com
Exam traps
Each one states something that sounds right. Open it to see what is actually true.
1.Table history is kept for 30 days, so you can always time travel 30 days back.Why is that wrong?
Time travel also needs the version's data files. VACUUM can remove those after the 7-day deletedFileRetentionDuration default, so both settings have to be raised to reach further back.
Covered in How far back you can travel
2.RESTORE just rewinds the table pointer, so downstream consumers are not affected.Why is that wrong?
RESTORE is a data-changing commit (dataChange = true). Downstream readers, such as streaming jobs, may process the restored files again as new data.
Covered in Rolling the whole table back with RESTORE
Practise it for real
Make a mistake on a scratch Delta table, then use time travel to inspect and repair it
1.Run DESCRIBE HISTORY on a scratch Delta table you own, after making at least two writes and then a DELETE.
Why: You need the version number and timestamp of the state you want to go back to.
You should see: One row per write, newest first, with version, timestamp, operation and userName columns.
2.Run SELECT count(*) on the table, then on the same table with VERSION AS OF set to the version just before the DELETE.
Why: This confirms that the old version still contains the rows the DELETE removed.
You should see: The historical count is higher than the current count.
3.Run the same historical query again using the @v shorthand, for example table_name@v<version>.
Why: This shows that @v and VERSION AS OF read the same version.
You should see: The same count as the VERSION AS OF query.
4.Run RESTORE TABLE on the table TO VERSION AS OF that version, then run DESCRIBE HISTORY LIMIT 1.
Why: RESTORE rolls the table back by committing a new version. It does not erase history.
You should see: A metrics row from RESTORE, and a newest history entry with operation RESTORE.
Stuck? Get a nudge
If RESTORE fails with a permissions error, check that you have MODIFY on the table.
Sources
Every claim above is drawn from one of these pages, quoted as it was written on the date shown.
- 1.https://docs.databricks.com/aws/en/tables/historyOfficial docs
“To query a previous table version, you must retain both the log and the data files for that version”
↩︎ How far back you can travel“determines the threshold VACUUM uses to remove data files no longer referenced in the current table version. The default is interval 7 days.”
↩︎ How far back you can travel“Table history retention is determined by the table setting logRetentionDuration, which is 30 days by default.”
↩︎ How far back you can travel“time travel queries are blocked if they request a version older than the deletedFileRetentionDuration table property (default 7 days)”
↩︎ How far back you can travel“For Unity Catalog managed tables, this applies to Databricks Runtime 12.2 and above.”
↩︎ How far back you can travel“Increasing data retention threshold can cause your storage costs to go up, as more data files are maintained.”
↩︎ How far back you can travel“To fix accidental deletes to a table for the user 111”
↩︎ Using an old version as a query source“To query the number of new customers added over the last week”
↩︎ Using an old version as a query source“To restore by timestamp, use the formats yyyy-MM-dd HH:mm:ss or yyyy-MM-dd.”
↩︎ Rolling the whole table back with RESTORE“After data files are deleted, manually or by VACUUM, you can't restore a table to an older version that references those files.”
↩︎ Rolling the whole table back with RESTORE“Log entries added by the RESTORE command contain dataChange set to true.”
↩︎ Rolling the whole table back with RESTORE“Restore is a data-changing operation and might result in duplicate data for downstream workloads.”
↩︎ Exam trap 2“Use only the past 7 days for time travel operations unless you have set both data and log retention configurations to a larger value.”
↩︎ Prediction“to access 30 days of historical data, set delta.deletedFileRetentionDuration = "interval 30 days", which matches the default setting for delta.logRetentionDuration.”
↩︎ Checkpoint“USING my_table TIMESTAMP AS OF date_sub(current_date(), 1) source”
↩︎ Checkpoint“To restore a table, you must have MODIFY permission for the table.”
↩︎ Checkpoint - 2.
“If predictive optimization is enabled, Databricks automatically triggers the VACUUM operation as part of its optimization process.”
↩︎ How far back you can travel“you lose the ability to time travel back to a version older than the specified data retention period.”
↩︎ Exam trap 1 - 3.
“Restores a Delta table to an earlier state. Restoring to an earlier version number or a timestamp is supported.”
↩︎ Rolling the whole table back with RESTORE