Sub topic 6
Undoing a bad change with RESTORE
This chapter covers rolling back a Delta table with RESTORE TABLE. It assumes you can read DESCRIBE HISTORY output and query a table with VERSION AS OF or TIMESTAMP AS OF. You should also know basic SQL and what a Delta table is. Earlier chapters covered those topics.
What RESTORE does
Time travel lets you read an old version of a table. It does not change the table. RESTORE TABLE goes one step further. It makes the current table match an earlier version.
Delta Lake does this without rewriting history. It adds a new commit to the transaction log. That commit re-adds the data files that belonged to the earlier version and removes the files that are not part of it. The old commits stay in the log, and the restore is one more entry in the history.
RESTORE TABLE main.sales.orders TO VERSION AS OF 5;You can also restore to a point in time:
RESTORE TABLE main.sales.orders TO TIMESTAMP AS OF '2026-11-01 00:00:00';A worked recovery
Suppose someone runs a DELETE with a wrong filter and removes most of the rows in main.sales.orders. Follow these steps to recover.
Step 1: find the bad commit
Look at the recent history.
DESCRIBE HISTORY main.sales.orders LIMIT 10;Find the row whose operation is DELETE. Say it is version 8. The last good state is then version 7. Check the operationMetrics column of that row. A numDeletedRows value much larger than you intended confirms the diagnosis.
Step 2: check the target version
Do not restore blindly. Query the version you plan to restore to.
SELECT COUNT(*) FROM main.sales.orders VERSION AS OF 7;You can also compare it with the current state, as shown in the previous chapter.
SELECT * FROM main.sales.orders VERSION AS OF 7
EXCEPT
SELECT * FROM main.sales.orders;The result lists the rows that the bad delete removed.
Step 3: restore
RESTORE TABLE main.sales.orders TO VERSION AS OF 7;The command returns a single row of metrics. It reports how many files were removed and restored, and the table size before and after.
Step 4: confirm in the history
DESCRIBE HISTORY main.sales.orders LIMIT 3;A new row appears at the top with operation set to RESTORE. The version number is 9, not 7. The earlier versions, including the bad DELETE at version 8, are still listed.
Because the bad commit is still in the history, you can undo a restore too. If you restored to the wrong version, restore again to version 8, or to whichever version you actually want.
Restoring from PySpark
The same command works from Python through spark.sql.
spark.sql("RESTORE TABLE main.sales.orders TO VERSION AS OF 7")
history = spark.sql("DESCRIBE HISTORY main.sales.orders").limit(3)
history.select("version", "timestamp", "operation").show(truncate=False)Common errors and gotchas
The following table lists the problems you are most likely to meet.
| Problem | Cause | What to do |
|---|---|---|
| Restore fails because files are missing | `VACUUM` already deleted the data files of the target version | Pick a newer version that still has its files. Data that was vacuumed cannot be recovered with `RESTORE`. |
| Restore fails because the version is not found | The commit entry was removed after `delta.logRetentionDuration` passed | Check `DESCRIBE HISTORY` for versions that still exist. |
| Permission error | You lack `MODIFY` on the table | Ask the table owner for the privilege. In Unity Catalog, `RESTORE` needs write access. |
| Restored table lacks recent good writes | Restore reverts everything after the target version, not only the bad change | Save the good rows first, then re-apply them after the restore. |
The last row is the most common surprise. Restore is not selective. If versions 8, 9 and 10 contain one bad commit and two good ones, restoring to version 7 discards all three. To keep the good changes, copy the rows you need into a temporary table first, or use the EXCEPT comparison to find them.
Summary of the workflow
- Read the history and find the last good version.
- Query that version to confirm it holds the data you expect.
- Compare it with the current table to see what you would lose and gain.
- Run
RESTORE TABLE ... TO VERSION AS OF. - Check that a
RESTOREcommit appears in the history.
Exercise
A table has versions 0 to 10. Version 6 is a bad UPDATE. Versions 7 to 10 are good appends. You run RESTORE TABLE t TO VERSION AS OF 5. What is the latest version number afterward, and what happens to the rows added in versions 7 to 10?
Show answer
The restore creates version 11, so the latest version is 11. The table content matches version 5, so the rows appended in versions 7 to 10 are no longer in the current table. They remain readable with VERSION AS OF 10 as long as their files have not been vacuumed. To keep them, copy them out before restoring or re-insert them afterward.
Exercise
After a restore to version 7, a colleague says the bad DELETE at version 8 is gone from the history. Is that correct? How can you check?
Show answer
No. RESTORE adds a new commit and does not remove earlier ones. Run DESCRIBE HISTORY on the table. Version 8 still appears, followed by a new RESTORE row at version 9.