Home / Data Platform / Lakehouse

Data PlatformRunbookIntermediate

Bad write to an Iceberg table: roll back to a snapshot with Spark procedures

A job overwrote or deleted the wrong rows in an Apache Iceberg table. Find the last good snapshot, verify it, roll back with Spark procedures and stop snapshot expiry from removing your way back.

Deepak Bhardwaj7 min read

Applies toApache Iceberg 1.x with Spark 3.4/3.5 (Iceberg SQL extensions enabled); Hive, Glue, REST and Nessie catalogs

Symptom

A pipeline ran with a bad filter, a wrong INSERT OVERWRITE, or a DELETE without the WHERE clause you meant. Dashboards now show missing or doubled numbers, and the table is an Apache Iceberg table.

Every Iceberg commit creates a new snapshot. The bad data is in the newest snapshot or snapshots. The older ones are still there until snapshot expiry removes them. Rolling back moves the table's current pointer to an older snapshot. It is a metadata operation: no data files are rewritten, and it finishes in seconds.

Caution

The clock is running. If a scheduled expire_snapshots job runs before you roll back, the good snapshot can be gone. Pause maintenance jobs on this table first.

1. Stop the damage

  1. Pause the job that made the bad write, and any job that writes to the table. A writer that keeps committing builds new snapshots on top of the bad state.
  2. Pause expire_snapshots, remove_orphan_files and compaction jobs for the table.
  3. Tell downstream owners. Consumers that already read the bad snapshot may need reprocessing after the fix.

The examples use a Spark catalog named prod and a table prod.sales.orders. All procedures need the Iceberg Spark SQL extensions (spark.sql.extensions=org.apache.iceberg.spark.extensions.IcebergSparkSessionExtensions).

2. Find the last good snapshot

Iceberg exposes table history as metadata tables:

-- What committed, when, and what it did
SELECT committed_at,
       snapshot_id,
       parent_id,
       operation,
       summary['added-records']   AS added_records,
       summary['deleted-records'] AS deleted_records,
       summary['spark.app.id']    AS spark_app
FROM prod.sales.orders.snapshots
ORDER BY committed_at DESC
LIMIT 20;
-- Which snapshots were current, in order, and whether they are ancestors of now
SELECT made_current_at, snapshot_id, parent_id, is_current_ancestor
FROM prod.sales.orders.history
ORDER BY made_current_at DESC
LIMIT 20;

Look for the commit that matches the incident: an overwrite or delete operation at the time the bad job ran, often with a large deleted-records count. The snapshot you want is that commit's parent_id. Summary keys vary by engine and version; check what your build writes.

Read the candidate snapshot before trusting it

Time travel reads a snapshot without changing anything:

SELECT count(*) FROM prod.sales.orders VERSION AS OF 4871203374659123456;
SELECT count(*) FROM prod.sales.orders;   -- current, bad state

-- Spot-check the rows the incident is about
SELECT order_date, count(*)
FROM prod.sales.orders VERSION AS OF 4871203374659123456
WHERE order_date >= DATE '2026-11-10'
GROUP BY order_date
ORDER BY order_date;

You can also travel by time: SELECT ... FROM prod.sales.orders TIMESTAMP AS OF '2026-11-18 02:00:00'.

Pin it with a tag

Before changing anything, tag the good snapshot so expiry cannot remove it while you work:

ALTER TABLE prod.sales.orders CREATE TAG `pre_incident_2026_11_18`
AS OF VERSION 4871203374659123456 RETAIN 30 DAYS;

Check it with SELECT * FROM prod.sales.orders.refs;.

3. Choose the right recovery

Situation Use Notes
Only bad commits since the good snapshot rollback_to_snapshot Snapshot must be an ancestor of the current state
You know the time, not the ID rollback_to_timestamp Rolls back to the snapshot current at that time
Target is not an ancestor (e.g. after an earlier rollback) set_current_snapshot No ancestor requirement; also accepts a tag or branch with ref
Good appends landed after the bad commit and must be kept Roll back, then cherrypick_snapshot the good ones Only append and dynamic overwrite snapshots can be cherry-picked
Bad write touched one partition, the rest has newer good data Rewrite that slice from time travel See section 5

4. Roll back

CALL prod.system.rollback_to_snapshot('sales.orders', 4871203374659123456);

Or by time:

CALL prod.system.rollback_to_timestamp('sales.orders', TIMESTAMP '2026-11-18 02:00:00.000');

Both return previous_snapshot_id and current_snapshot_id. Save previous_snapshot_id. It is your undo. If the rollback was wrong, you can return to it:

CALL prod.system.set_current_snapshot('sales.orders', 5520918837261904512);
-- or pin by tag
CALL prod.system.set_current_snapshot(table => 'sales.orders', ref => 'pre_incident_2026_11_18');

Keep good commits that came after the bad one

If a correct append committed after the bad overwrite, roll back to before the bad commit and re-apply the good append:

CALL prod.system.rollback_to_snapshot('sales.orders', 4871203374659123456);
CALL prod.system.cherrypick_snapshot('sales.orders', 6193047782215530880);

Cherry-picking creates a new snapshot with the changes from the picked one. Check counts after each step.

5. Partial repair instead of a full rollback

When the bad write hit one slice and other slices have newer good data you can't lose, repair only that slice from a time travel read. A MERGE or a scoped overwrite works; for a partitioned table with dynamic overwrite:

SET spark.sql.sources.partitionOverwriteMode = dynamic;

INSERT OVERWRITE prod.sales.orders
SELECT * FROM prod.sales.orders VERSION AS OF 4871203374659123456
WHERE order_date = DATE '2026-11-17';

Warning

Without partitionOverwriteMode = dynamic, Spark uses static overwrite. With no PARTITION clause, a static INSERT OVERWRITE replaces the whole table with just the slice you selected. Test the statement on a copy, or check added-records and deleted-records on the new snapshot immediately after it commits.

6. Verify and resume

SELECT snapshot_id, operation, committed_at
FROM prod.sales.orders.snapshots ORDER BY committed_at DESC LIMIT 3;

SELECT made_current_at, snapshot_id, is_current_ancestor
FROM prod.sales.orders.history ORDER BY made_current_at DESC LIMIT 5;
  • Row counts and spot checks match the numbers you saw in step 2.
  • Readers on other engines (Trino, Athena, Flink, Snowflake) see the new current snapshot. Engines that cache metadata may need a refresh; check each engine's docs.
  • Incremental and streaming readers that already consumed the bad snapshots need attention. Rolling back does not "un-send" what they processed, so reprocess the affected range downstream.
  • Fix the bad job, then resume writers. Resume maintenance last.

Snapshots after the rollback point stay in table metadata until they expire. That is useful for forensics. Drop the incident tag once you no longer need it:

ALTER TABLE prod.sales.orders DROP TAG `pre_incident_2026_11_18`;

Common errors

Error or symptom Cause Fix
Cannot roll back to snapshot, not an ancestor of the current state (wording varies) Target is on a different line of history, often after an earlier rollback Use set_current_snapshot
Cannot find snapshot / snapshot ID missing from snapshots Expired by expire_snapshots Restore from backups or a copy; the metadata is gone
Procedure ... not found or CALL parse error Iceberg SQL extensions not loaded, or wrong catalog name Set spark.sql.extensions; call <catalog>.system.<procedure>
Rollback succeeded but readers show old data Engine metadata cache Refresh or wait for the engine's cache TTL
Cherry-pick rejected Snapshot is an overwrite or delete Re-run that write instead

Prevent it next time

  • Retention that covers detection time. If bad data is usually spotted the next morning, keep at least a few days of snapshots. Set retain_last and older_than in your expire_snapshots job to match, and check the current defaults in your Iceberg version.
  • Tag before risky operations. Have backfill and overwrite jobs create a tag (CREATE TAG ... RETAIN 7 DAYS) before they write. Rollback is then one set_current_snapshot call with ref.
  • Write-audit-publish. Set write.wap.enabled = true on the table, write into a branch (SET spark.wap.branch = audit), run data quality checks against the branch, then publish with CALL prod.system.fast_forward('sales.orders', 'main', 'audit').
  • Alert on unusual commits. A daily query on .snapshots that flags overwrite or delete operations with large deleted-records catches most incidents before users do.
  • Keep a one-page runbook with the catalog name, the maintenance job names to pause, and the procedure calls above.