What you will be able to do
- Query an earlier Delta table version by version number, by timestamp, or with the @ shorthand
- Use a version number or timestamp from DESCRIBE HISTORY in a time travel query
- Explain how VACUUM and the deletedFileRetentionDuration and logRetentionDuration settings limit how far back you can travel
- Choose between RESTORE TABLE and patching rows from an old version with INSERT or MERGE
Key concept
Table version — Every write that Delta commits produces a new numbered snapshot of the whole table. Time travel reads one of those snapshots instead of the current one, as long as its log entries and data files still exist.
1.Why a Delta table has a past you can query
A Delta table is a sequence of committed versions, and its current rows are only the latest one. Delta Lake and its transaction log records every write as a commit, and each commit becomes a numbered table version. Version 0 is the write that created the table. After that, an INSERT might make version 1 and a DELETE version 2. Writing a new version does not remove the older ones, so the table can still answer the question "what did you contain at version 2?"
Time travel is the feature that answers that question. You add a clause after the table name in a query, and Databricks reads that earlier snapshot instead of the latest one. The current table does not change. The query just sees the table as it was at that point.
Analysts use time travel in four situations:
- Re-creating a report, analysis or model output exactly as it was first produced. This matters for debugging and for audits in regulated industries.
- Writing temporal queries that compare the table now with the table at an earlier point.
- Fixing mistakes in the data by reading the rows as they were before a bad write.
- Giving a set of queries one consistent snapshot of a fast-changing table, so they all see the same version.
Time travel only reaches back a limited distance, so it does not replace an archive. Table history is meant for recent operations. Don't treat it as a long-term backup for data you're required to keep.
Checkpoint 1 of 8· Check yourself
An analyst plans to meet an audit requirement by keeping last year's sales data available only through the Delta table's version history. What is wrong with this plan?
Table history is meant for recent operations, and time travel can only reach back a limited distance. Data you are required to keep needs a real archive. The other options are wrong: new writes leave older versions in place, and a time travel query does not change the current table.
“Don't use table history as a long-term backup solution for data archival.”Source: docs.databricks.com
Sources1
2.Querying a version by number, by timestamp, or with @
You write time travel as a clause after the table name, in one of two forms. VERSION AS OF takes a version number. TIMESTAMP AS OF takes a point in time and returns the snapshot that was current at that moment. Both forms work anywhere the table is named as a table reference, including as the source of a MERGE.
SELECT * FROM people10m TIMESTAMP AS OF '2018-10-18T22:15:12.013Z';
SELECT * FROM people10m VERSION AS OF 123;Choose the form based on what you know. Sometimes you know *when* something went wrong but not which commit caused it, such as a nightly job that corrupted a column at some point yesterday. TIMESTAMP AS OF lets you inspect the table at a moment you choose, without looking anything up first. When you need one exact commit, use DESCRIBE HISTORY. It lists each commit's version number, timestamp and operation. Take the number from its version column and pass it to VERSION AS OF. Rows don't carry a version number, so filtering the live table with WHERE version = 45 doesn't read version 45.
The timestamp can be a literal or a calculated value. Accepted forms include:
- a full timestamp string such as
'2018-10-18T22:15:12.013Z' - a plain date string such as
'2018-10-18' - a
cast(... as timestamp) - expressions such as
current_timestamp() - interval 12 hoursordate_sub(current_date(), 1)
Anything that is a timestamp or can be cast to one works. Neither the timestamp nor the version can be a subquery. Look the value up first, then type it into the query.
The @ shorthand puts the same information into the table name. For a version, add @v and the number, so events@v123 means version 123. A timestamp after @ must use the compact yyyyMMddHHmmssSSS format, which runs from year down to milliseconds with no separators. For example, @20190101000000000 is midnight on 1 January 2019.
-- Timestamp version
SELECT * FROM people10m@20190101000000000
-- Version number
SELECT * FROM people10m@v123| Syntax | What you supply | Use it when |
|---|---|---|
| events VERSION AS OF 123 | A long version number from DESCRIBE HISTORY | You need one exact commit |
| events TIMESTAMP AS OF '2018-10-18T22:15:12.013Z' | A string or expression that can be cast to a timestamp | You know the moment, not the commit |
| events@v123 | v followed by the version number | You want the version inside the table name |
| events@20190101000000000 | A timestamp in yyyyMMddHHmmssSSS format | You want the timestamp inside the table name |
A past snapshot is just another table reference, so one statement can use it alongside the current table. For example, count the distinct userId values in my_table, then subtract the count from my_table TIMESTAMP AS OF date_sub(current_date(), 7). The difference is the number of new customers added in the last week.
Checkpoint 2 of 8· Fill the gap
The second query should use the @ shorthand to read version 123 of people10m. Which token completes it?
-- Timestamp version
SELECT * FROM people10m@20190101000000000
-- Version number
SELECT * FROM people10m@ ? In the @ shorthand, a version number is written as v followed by the number, so people10m@v123 means version 123. Digits straight after @ are read as a timestamp in yyyyMMddHHmmssSSS format, like the 17-digit value in the first query.
Source: docs.databricks.comCheckpoint 3 of 8· Exam question
An analyst suspects a nightly job corrupted the orders table's status column sometime yesterday, but does not know the exact version number when the corruption started. Which approach lets them inspect the table's contents as of a specific date and time without first looking up a version number?
Correct answer: A — Run SELECT * FROM orders TIMESTAMP AS OF '2026-09-14T00:00:00.000Z'; to read the table snapshot as it existed at that timestamp
- A. Correct: the
TIMESTAMP AS OFclause lets a query target the table state as of a specific date and time directly, so the analyst does not need to first resolve a version number before inspecting historical rows. - B. Incorrect:
VERSION AS OF 0returns only the table's initial state, not yesterday's state, and manually diffing every row against the current table is far less direct than querying a timestamp. - C. Incorrect:
DESCRIBE HISTORYlists operations, users, and timestamps for each commit, but its metrics columns summarize row counts and file changes, not the actual column values, so it cannot substitute for querying the data. - D. Incorrect:
VACUUMpermanently deletes stale data files that are no longer referenced by the retention window; it does not restore a table to a prior state and running it can actually remove files needed for time travel.
Checkpoint 4 of 8· Exam question
Before comparing today's inventory table against last Friday's snapshot, an analyst runs DESCRIBE HISTORY inventory; and sees a version column alongside timestamp and operation. Which statement correctly describes how to use the version value returned there in a subsequent query?
Correct answer: A — Pass the version number after VERSION AS OF in a SELECT, such as SELECT * FROM inventory VERSION AS OF 45;, to read that exact committed snapshot
- A. Correct:
VERSION AS OFaccepts the long integer version number shown byDESCRIBE HISTORYand returns the table exactly as it existed after that commit was written. - B. Incorrect: Delta tables do not store a queryable
versioncolumn on individual rows; version is metadata about the table's commit history in the transaction log, not a per-row attribute available to aWHEREclause. - C. Incorrect: version numbers are sequential commit identifiers, not a function of elapsed time or checkpoint frequency, so there is no arithmetic conversion from a version number to a timestamp.
- D. Incorrect:
VACUUMtakes a retention duration such asRETAIN 168 HOURS, not a version number, and it deletes stale unreferenced files rather than pruning commits after a given version.
3.How far back you can travel: retention and VACUUM
You can read a past version only while two things still exist: its entries in the transaction log and the data files it points to. Two separate processes delete these, and each one has its own retention setting. Whichever setting is shorter decides how far back you can go.
VACUUM removes old data files. When an UPDATE, DELETE or OPTIMIZE rewrites data, the replaced files are no longer part of the current version. They stay in storage so that older versions can still use them. VACUUM deletes data files that meet two conditions:
- They are not part of the latest table state.
- They were logically removed from the transaction log longer ago than the retention threshold.
That threshold is the table property delta.deletedFileRetentionDuration, which defaults to 7 days. After VACUUM runs, you can no longer time travel to versions older than that period. If predictive optimization is enabled, Databricks runs VACUUM automatically. Assume it will run even if nobody types the command.
Log entries expire on their own. Databricks removes log files automatically after it checkpoints table versions. How long table history is kept is set by delta.logRetentionDuration, which defaults to 30 days. On newer runtimes, two extra rules apply:
- Time travel queries are blocked outright when they ask for a version older than
deletedFileRetentionDuration. logRetentionDurationmust be at least as long asdeletedFileRetentionDuration.
These rules apply in Databricks Runtime 18.0 and above. For Unity Catalog managed tables, they apply in Databricks Runtime 12.2 and above.
| Table property | What it controls | Default |
|---|---|---|
| delta.deletedFileRetentionDuration | How long VACUUM keeps data files that the current version no longer references | interval 7 days |
| delta.logRetentionDuration | How long the table history (transaction log) is kept | interval 30 days |
In practice, plan on 7 days. Only use the past week for time travel unless both settings have been raised. To keep 30 days within reach, set delta.deletedFileRetentionDuration = "interval 30 days", which matches the log default. You can set both properties when you create the table, or later with ALTER TABLE. The trade-off is storage cost. Every data file that an older version needs is kept longer, so costs rise as you lengthen the window.
History is stored in the log, which is kept for 30 days by default, so the commit still shows up. The data files behind that version became eligible for VACUUM after 7 days and have been deleted. On current runtimes, a request for a version older than deletedFileRetentionDuration is blocked anyway. A version appearing in history doesn't mean you can still read it.
Checkpoint 5 of 8· Check yourself
Both retention properties on a Delta table are at their defaults. The team wants VERSION AS OF queries to keep working for versions up to 30 days old. Which single change does that?
Time travel needs both the log entries and the data files for a version. The log is already kept for 30 days by default, but VACUUM removes the data files after 7 days, so the file retention setting is the one to raise. VACUUM never rolls a table back. It only deletes files.
“For example, 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
4.Rolling back with RESTORE TABLE, or patching from an old version
A time travel SELECT reads the past but doesn't change the present. Suppose a bad batch job has overwritten a table and the table itself must go back to how it was. The command for that is RESTORE TABLE. It takes the same VERSION AS OF or TIMESTAMP AS OF clause, and the keywords TABLE and TO are optional. To restore by timestamp, use the format yyyy-MM-dd HH:mm:ss or yyyy-MM-dd.
RESTORE TABLE target_table TO VERSION AS OF <version>;
RESTORE TABLE target_table TO TIMESTAMP AS OF <timestamp>;RESTORE doesn't erase the versions that came after the target. Instead it adds a new commit on top of the history. Suppose a table is at version 2 and you restore version 1. The result is version 3, with the operation RESTORE(version=1). Its log entries remove the current files and add the old ones back. Because a restore is itself a version, you can restore a table that was already restored, and you can also restore a cloned table. When it finishes, RESTORE returns one row of metrics, such as table_size_after_restore, num_removed_files and num_restored_files.
RESTORE comes with three constraints:
- You need MODIFY permission on the table.
- You can't restore to a version whose data files have already been deleted, whether by hand or by VACUUM. The retention window that limits time travel queries limits RESTORE too.
- The files it adds back are logged with
dataChangeset to true. Downstream consumers, such as a Structured Streaming job reading the table, treat them as new data and may process the same rows a second time.
Patching is the alternative when only some rows are wrong. Instead of moving the whole table back, you read the old snapshot with time travel and write only the affected rows into the current version. To undo an accidental delete for one user, use an INSERT that selects that user's rows from yesterday's snapshot. To reverse a bad update, use a MERGE with yesterday's snapshot as its source. Any changes made since then stay in place.
INSERT INTO my_table
SELECT * FROM my_table TIMESTAMP AS OF date_sub(current_date(), 1)
WHERE userId = 111MERGE 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 *| Aspect | RESTORE TABLE | INSERT or MERGE from an old version |
|---|---|---|
| Scope | The whole table returns to the chosen version | Only the rows you select are rewritten |
| Good changes made since that version | Undone along with the bad ones | Kept |
| Best when | A whole write was bad, such as an overwrite with a broken transformation | A small set of rows was deleted or updated by mistake |
| Effect on history | Adds a RESTORE commit, and earlier versions stay in the history | Adds an ordinary write commit |
Checkpoint 6 of 8· Fill the gap
Yesterday, user 111's rows were deleted by mistake, and other good changes have been made to my_table since then. Which keyword completes this statement so it brings back only that user's rows?
? INTO my_table
SELECT * FROM my_table TIMESTAMP AS OF date_sub(current_date(), 1)
WHERE userId = 111Only a few rows are wrong, so you patch them. INSERT INTO selects user 111's rows from yesterday's snapshot and writes them into the current version, and later changes stay in place. RESTORE would move the whole table back and undo the good changes too. MERGE needs a USING ... ON clause, not a plain SELECT.
Source: docs.databricks.comCheckpoint 7 of 8· Check yourself
A Structured Streaming job processes each new update to a table. You run RESTORE TABLE to take that table back to version 1. What should the job's owner expect?
RESTORE adds a new commit that adds the old files back with dataChange set to true. Downstream streaming readers count that as new data, so the same rows can be processed twice. RESTORE also keeps the later versions in the history instead of deleting them.
“Restore is a data-changing operation and might result in duplicate data for downstream workloads.”Source: docs.databricks.com
Checkpoint 8 of 8· 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 TABLEreverts 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 OFonly 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_versiontable 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 OFdoes not accept an interval expressed in versions, andCOPY INTOis designed for ingesting external files, not for reverting a table to its own earlier state.
Sources
(4)
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
“Time travel supports querying previous table versions based on timestamp or table version (as recorded in the transaction log).”
↩︎ Why a Delta table has a past you can query“Providing snapshot isolation for a set of queries for fast changing tables.”
↩︎ Why a Delta table has a past you can query“Don't use table history as a long-term backup solution for data archival.”
↩︎ Why a Delta table has a past you can query“version is a long value that can be obtained from the output of DESCRIBE HISTORY table_spec.”
↩︎ Querying a version by number, by timestamp, or with @“To query the number of new customers added over the last week:”
↩︎ Querying a version by number, by timestamp, or with @“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: retention and VACUUM“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: retention and VACUUM“Log files are removed automatically after checkpointing table versions.”
↩︎ How far back you can travel: retention and VACUUM“Increasing data retention threshold can cause your storage costs to go up, as more data files are maintained.”
↩︎ How far back you can travel: retention and VACUUM“To restore a table, you must have MODIFY permission for the table.”
↩︎ Rolling back with RESTORE TABLE, or patching from an old version“After data files are deleted, manually or by VACUUM, you can't restore a table to an older version that references those files.”
↩︎ Rolling back with RESTORE TABLE, or patching from an old version“Log entries added by the RESTORE command contain dataChange set to true.”
↩︎ Rolling back with RESTORE TABLE, or patching from an old version“To restore by timestamp, use the formats yyyy-MM-dd HH:mm:ss or yyyy-MM-dd.”
↩︎ Rolling back with RESTORE TABLE, or patching from an old version“each operation that modifies a table creates a new table version”
↩︎ Key concept“Use only the past 7 days for time travel operations unless you have set both data and log retention configurations to a larger value.”
↩︎ Exam trap 2“Restore is a data-changing operation and might result in duplicate data for downstream workloads.”
↩︎ Exam trap 4“For example, to access 30 days of historical data, set delta.deletedFileRetentionDuration = "interval 30 days", which matches the default setting for delta.logRetentionDuration.”
↩︎ Checkpoint“Restore is a data-changing operation and might result in duplicate data for downstream workloads.”
↩︎ Checkpoint - 2.https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-table-referenceOfficial docs
“Neither timestamp_expression nor version can be subqueries.”
↩︎ Querying a version by number, by timestamp, or with @“Use the @ syntax to specify the timestamp or version. The timestamp must be in yyyyMMddHHmmssSSS format.”
↩︎ Querying a version by number, by timestamp, or with @“You can specify a version after @ by prepending a v to the version.”
↩︎ Exam trap 1“version is a long value that can be obtained from the output of DESCRIBE HISTORY table_spec.”
↩︎ Prediction - 3.
“Delta table data files are deleted according to the time they have been logically removed from Delta's transaction log plus retention hours”
↩︎ How far back you can travel: retention and VACUUM“If predictive optimization is enabled, Databricks automatically triggers the VACUUM operation as part of its optimization process.”
↩︎ How far back you can travel: retention and VACUUM“Databricks strongly recommends setting a retention interval of at least 7 days.”
↩︎ How far back you can travel: retention and VACUUM“you lose the ability to time travel back to a version older than the specified data retention period.”
↩︎ Exam trap 3 - 4.
“Restores a Delta table to an earlier state. Restoring to an earlier version number or a timestamp is supported.”
↩︎ Rolling back with RESTORE TABLE, or patching from an old version