CertSafari
    Databricks Certified Data Analyst Associate· Lessons

    Domain 4 · Lesson 17/39

    Delta Lake time travel: query and restore past table versions

    Use Delta Lake's time travel to access and query historical data versions.

    17 min read
    2.22% of exam
    4 sources
    Published 22 Oct 2026
    Docs as of 30 Sep 2026

    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?

    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.

    The same table read by timestamp and by version numbersql
    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 hours or date_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.

    The @ shorthand for a timestamp and for a version numbersql
    -- Timestamp version
    SELECT * FROM people10m@20190101000000000
    -- Version number
    SELECT * FROM people10m@v123
    Four ways to name a past snapshot of the same table
    SyntaxWhat you supplyUse it when
    events VERSION AS OF 123A long version number from DESCRIBE HISTORYYou need one exact commit
    events TIMESTAMP AS OF '2018-10-18T22:15:12.013Z'A string or expression that can be cast to a timestampYou know the moment, not the commit
    events@v123v followed by the version numberYou want the version inside the table name
    events@20190101000000000A timestamp in yyyyMMddHHmmssSSS formatYou 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@ ? 

    Checkpoint 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?

    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?

    Sources21

    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.
    • logRetentionDuration must be at least as long as deletedFileRetentionDuration.

    These rules apply in Databricks Runtime 18.0 and above. For Unity Catalog managed tables, they apply in Databricks Runtime 12.2 and above.

    The two retention settings that limit time travel
    Table propertyWhat it controlsDefault
    delta.deletedFileRetentionDurationHow long VACUUM keeps data files that the current version no longer referencesinterval 7 days
    delta.logRetentionDurationHow long the table history (transaction log) is keptinterval 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.

    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?

    Sources13

    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 by version number or by timestampsql
    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 dataChange set 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.

    Re-inserting one user's deleted rows from yesterday's snapshotsql
    INSERT INTO my_table
      SELECT * FROM my_table TIMESTAMP AS OF date_sub(current_date(), 1)
      WHERE userId = 111
    Overwriting incorrectly updated rows with yesterday's valuessql
    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 *
    Choosing between RESTORE and a time travel patch
    AspectRESTORE TABLEINSERT or MERGE from an old version
    ScopeThe whole table returns to the chosen versionOnly the rows you select are rewritten
    Good changes made since that versionUndone along with the bad onesKept
    Best whenA whole write was bad, such as an overwrite with a broken transformationA small set of rows was deleted or updated by mistake
    Effect on historyAdds a RESTORE commit, and earlier versions stay in the historyAdds 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 = 111

    Checkpoint 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?

    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?

    Sources41

    Sources

    (4)

    Every claim above is drawn from one of these pages, quoted as it was written on the date shown.

    1. 1.
      “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. 2.
      “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. 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. 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

    Spotted a mistake, or was something unclear? Tell us.