Sub topic 12
Table History, Time Travel and RESTORE
This chapter assumes you can run SQL and PySpark in a Databricks notebook, and that you know how the Delta transaction log records each commit as a numbered table version. It uses the main.sales.orders table from earlier chapters. You will inspect a table's history, read older versions, roll a table back, and learn what limits how far back you can go.
What the history records
Every successful write to a Delta table creates a new version. Version 0 is the first commit, and each later commit adds 1. The transaction log stores a commit record for each version. The record holds the operation name, a timestamp, the user, and metrics such as the number of rows written.
DESCRIBE HISTORY reads those records and returns them as a table, with the newest version first.
DESCRIBE HISTORY main.sales.orders;Some of the columns you will use most:
| Column | Meaning |
|---|---|
| `version` | The table version created by the commit |
| `timestamp` | When the commit finished |
| `userName` | The user or service principal that ran the write |
| `operation` | The kind of change, such as `WRITE`, `UPDATE`, `DELETE`, `MERGE`, `OPTIMIZE` or `RESTORE` |
| `operationParameters` | Details such as the predicate of a `DELETE` or the write mode |
| `operationMetrics` | Row and file counts, for example `numOutputRows` or `numTargetRowsUpdated` |
To look at only the latest few commits, add a limit.
DESCRIBE HISTORY main.sales.orders LIMIT 5;You can also get the history as a DataFrame and filter it like any other table.
from delta.tables import DeltaTable
history = DeltaTable.forName(spark, "main.sales.orders").history()
(history
.select("version", "timestamp", "operation", "operationMetrics")
.filter("operation IN ('UPDATE', 'DELETE', 'MERGE')")
.show(truncate=False))The history answers practical questions. Who deleted these rows? When did the nightly merge last run? How many rows did it change? Always check it before you decide to roll anything back.
Time travel: reading an earlier version
Time travel means querying the table as it was at an earlier version or time. Delta does not copy data to make this possible. Old data files stay on storage after an UPDATE or DELETE. The log marks them as removed in the newer version, but an older version still points to them.
You can name the version directly.
SELECT * FROM main.sales.orders VERSION AS OF 3;Or you can name a timestamp. Delta reads the latest version committed at or before that time.
SELECT * FROM main.sales.orders TIMESTAMP AS OF '2026-12-03 09:00:00';In Python, use the reader options versionAsOf and timestampAsOf.
old_df = (spark.read
.option("versionAsOf", 3)
.table("main.sales.orders"))
old_df.show()Time travel is read-only. It changes nothing in the table.
Comparing two versions
A common use is to see what a bad job changed. Compare an earlier version with the current one using EXCEPT.
SELECT * FROM main.sales.orders VERSION AS OF 3
EXCEPT
SELECT * FROM main.sales.orders;This returns rows that existed in version 3 but are missing or different now. Swap the two queries to find rows that are new or changed.
Version numbers and timestamps
Use a version number when you know it from DESCRIBE HISTORY. It is exact. Use a timestamp when you only know roughly when something went wrong. Two details cause confusion:
- Timestamps are interpreted in the session time zone unless you give an explicit offset. Check
spark.sql.session.timeZoneif results look off by some hours. - A timestamp earlier than the first commit, or later than the last commit, raises an error.
Rolling back with RESTORE
RESTORE sets the table back to the state of an earlier version or timestamp.
RESTORE TABLE main.sales.orders TO VERSION AS OF 3;RESTORE TABLE main.sales.orders TO TIMESTAMP AS OF '2026-12-03 09:00:00';RESTORE does not rewrite history. It creates a new commit that re-adds the data files that were live in the target version and removes the ones that are not. If the table was at version 7 and you restore to version 3, the table is now at version 8, and its contents match version 3.
The diagram below shows this sequence.
Because versions 4 to 7 stay in the log, you can still read them with time travel after the restore. You can even restore to version 7 later to undo the rollback, provided the files are still on storage.
Permissions and results
RESTORE needs MODIFY on the table. Output includes the restored version and metrics such as num_restored_files and num_removed_files. Read these to confirm the operation did what you expected.
Safe habits before restoring
- Run
DESCRIBE HISTORYand identify the exact version before the bad commit. - Query that version with
VERSION AS OFand check the rows. - Compare it with the current table to see what you will lose. A restore also discards good changes made after the target version.
- Run
RESTORE. - Run
DESCRIBE HISTORYagain and confirm the newRESTOREentry.
A targeted repair looks like this.
MERGE INTO main.sales.orders AS t
USING (
SELECT * FROM main.sales.orders VERSION AS OF 3
WHERE status = 'cancelled'
) AS s
ON t.order_id = s.order_id
WHEN MATCHED THEN UPDATE SET *
WHEN NOT MATCHED THEN INSERT *;Limits of time travel
Time travel only works while two things are still available: the commit records in the log, and the data files they point to. Two table properties control how long each is kept.
| Property | Default | Controls |
|---|---|---|
| `delta.logRetentionDuration` | 30 days | How long commit history is kept |
| `delta.deletedFileRetentionDuration` | 7 days | How long removed data files are kept before `VACUUM` may delete them |
In practice, the shorter limit applies. After VACUUM deletes files that are older than the retention period, a query on a version that needs those files fails, even if the history still lists that version. The error usually says that files cannot be found, or that the requested version is no longer available.
You can change retention for a table. A longer period lets you travel further back, but it keeps more files on storage and raises cost.
ALTER TABLE main.sales.orders SET TBLPROPERTIES (
'delta.logRetentionDuration' = 'interval 60 days',
'delta.deletedFileRetentionDuration' = 'interval 30 days'
);Set these before you need them. They do not bring back files that VACUUM has already removed.
Other things that affect old versions
- Schema changes. Time travel to a version returns the schema of that version. Columns added later do not appear.
OPTIMIZE. It adds a version and rewrites small files into larger ones. Earlier versions still read correctly untilVACUUMremoves the old small files.- Not a backup. The history lives next to the data on the same storage. If someone drops the table or deletes the storage location, history goes with it. For disaster recovery, use a separate copy, for example a
DEEP CLONEin another location. - Streaming and checkpoints. A stream that reads a Delta table tracks versions. Restoring the source table can make a stream see old data again or fail, so plan a restore with your streaming jobs in mind.
Common errors
| Symptom | Likely cause | What to do |
|---|---|---|
| Error that the version or files do not exist | `VACUUM` removed the files, or the log retention has passed | Pick a newer version. Raise retention for the future. |
| Timestamp error before the earliest commit | The timestamp predates version 0 | Check the first row of `DESCRIBE HISTORY`. |
| Restore result includes unwanted rollbacks | Other jobs wrote after the target version | Use a targeted `MERGE` repair instead. |
| Time-travel query returns the current data | The query used the current table name without `VERSION AS OF` or `TIMESTAMP AS OF` | Add the clause. |
Putting it together
The next example simulates a bad delete, finds it in the history, checks the old data, and restores the table.
DELETE FROM main.sales.orders WHERE status = 'shipped';
DESCRIBE HISTORY main.sales.orders LIMIT 3;
SELECT COUNT(*) AS rows_before_delete
FROM main.sales.orders VERSION AS OF 3;
RESTORE TABLE main.sales.orders TO VERSION AS OF 3;
SELECT COUNT(*) AS rows_after_restore FROM main.sales.orders;In this example, version 3 is the last version before the delete. Replace it with the version you find in your own history. The two counts should match.
Exercise
A table is at version 12. A bad job ran at version 10, and a valid job ran at version 11 and 12. You run RESTORE TABLE t TO VERSION AS OF 9. What version is the table at afterwards, and what happens to the changes from versions 11 and 12?
Show answer
The table is at version 13, because RESTORE adds a new commit. Its contents match version 9, so the valid changes from versions 11 and 12 are removed from the current table as well. They remain readable with VERSION AS OF 11 or 12 until VACUUM deletes their files. To keep them, repair only the bad rows from version 9 with MERGE.
Exercise
Your history shows version 5, but SELECT * FROM t VERSION AS OF 5 fails because files are missing. What most likely happened, and how do you prevent it for future tables?
Show answer
VACUUM deleted the data files for version 5 after they passed the deleted-file retention period (7 days by default), while the commit record is still in the log. Prevent it by raising delta.deletedFileRetentionDuration (and delta.logRetentionDuration if needed) before the data ages out, or by keeping a separate clone for long-term recovery.