Sub topic 12
Table history, time travel and RESTORE
This chapter assumes you can run SQL in a Databricks notebook, you know how the Delta transaction log records each commit, and you have already modified a table with UPDATE, DELETE or MERGE. Every example uses the main.sales.orders table from earlier chapters.
The working model
Each write to a Delta table creates a new table version. The transaction log stores versions as numbered commits, starting at 0. A version points to the set of data files that make up the table at that moment. Older data files stay in storage until VACUUM removes them, so Delta can rebuild any earlier version as long as its files still exist.
Three features use this model:
- History lists the commits.
- Time travel reads an earlier version.
- RESTORE makes an earlier version the current one.
Inspecting history
DESCRIBE HISTORY returns one row per commit, newest first.
DESCRIBE HISTORY main.sales.orders;Useful columns:
| Column | Meaning |
|---|---|
| `version` | The commit number |
| `timestamp` | When the commit finished |
| `userName` | Who ran the operation |
| `operation` | For example WRITE, UPDATE, DELETE, MERGE, OPTIMIZE |
| `operationMetrics` | Row and file counts, such as rows updated |
Check operation and operationMetrics before you pick a version. A DELETE that removed far more rows than you expected is easy to spot there.
Reading an earlier version
Use VERSION AS OF with a version number, or TIMESTAMP AS OF with a point in time.
SELECT * FROM main.sales.orders VERSION AS OF 3;
SELECT * FROM main.sales.orders TIMESTAMP AS OF '2026-12-03 09:00:00';In Python, pass the same choice as a reader option:
df_v3 = (spark.read
.format("delta")
.option("versionAsOf", 3)
.table("main.sales.orders"))
df_v3.show()Time travel is read-only. It does not change the table. You can compare versions to see what a bad job did:
SELECT * FROM main.sales.orders VERSION AS OF 3
EXCEPT
SELECT * FROM main.sales.orders;This query returns rows that existed in version 3 but are missing now.
Rolling back with RESTORE
RESTORE sets the table back to an earlier version or timestamp.
RESTORE TABLE main.sales.orders TO VERSION AS OF 3;RESTORE does not erase history. It writes a new commit whose data matches the chosen version, so the bad commits stay in the log. You can inspect them, and you can even restore forward again.
Retention limits
How far back you can go depends on two table properties:
| Property | Default | Controls |
|---|---|---|
| `delta.logRetentionDuration` | 30 days | How long commit history is kept |
| `delta.deletedFileRetentionDuration` | 7 days | How long VACUUM keeps removed data files |
The effective window for time travel is usually the shorter of the two, which is typically the 7-day file retention. Changing these properties to keep more history costs storage, because old files stay around.
ALTER TABLE main.sales.orders SET TBLPROPERTIES (
'delta.deletedFileRetentionDuration' = 'interval 30 days'
);Common errors
- Missing files after VACUUM. The version is still listed in history, but its data files are gone. Query a more recent version, or restore from a separate copy.
- Wrong version chosen. Version numbers go up with every commit, including OPTIMIZE and RESTORE. Read
operationin the history before you restore. - Expecting RESTORE to shrink history. It adds a commit instead.
Exercise
A job ran DELETE at version 7 and removed the wrong rows. The table is now at version 8, which was an OPTIMIZE. Which statement returns the table to its state before the DELETE, and what version does the table have afterwards?
Show answer
Run RESTORE TABLE main.sales.orders TO VERSION AS OF 6;. Version 6 is the last state before the DELETE. RESTORE writes a new commit, so the table is at version 9 afterwards, with the same data as version 6. This works only if VACUUM has not removed the data files for version 6.