skip to content

In Cloudflare D1, how do you use Time Travel to recover from an UPDATE run without a WHERE clause, and what does a restore do?

level: middleimportance: should knowfreq 40%

answer

  1. always on, nothing to schedule
  2. a minute becomes a bookmark
  3. time-travel info, then restore
  4. destructive, in place, undoable

basics

~20 s

Time Travel keeps D1's history for 30 days on Workers Paid (7 on Free). wrangler d1 time-travel restore with --timestamp or --bookmark overwrites the whole database in place, cancels in-flight queries and prints the previous bookmark for undoing it.

solid answer

~40 s

Time Travel is always on for D1 databases on the production storage backend, at no extra cost, and lets you restore to any minute within 30 days on Workers Paid or 7 days on Free. Pick a time just before the bad statement, check it with `wrangler d1 time-travel info <db> --timestamp=<time>`, which prints the matching bookmark, then run `wrangler d1 time-travel restore <db> --timestamp=<time>` or `--bookmark=<id>`, exactly one of the two. The restore is destructive: it overwrites the whole database in place, so writes made after that minute are lost too, and in-flight queries and transactions are cancelled with an error. It prints the previous bookmark, so a restore can itself be undone. Exporting the current state with `wrangler d1 export --remote` first lets you salvage rows written after the mistake.

code

bash · 8 lines
bash
# 1. Find the bookmark for a minute before the bad UPDATE
npx wrangler d1 time-travel info catalogue --timestamp="2026-10-10T14:05:00Z"

# 2. Keep the current state, so rows written since can be salvaged
npx wrangler d1 export catalogue --remote --output=./before-restore.sql

# 3. Restore in place: destructive, asks for confirmation, prints the previous bookmark
npx wrangler d1 time-travel restore catalogue --timestamp="2026-10-10T14:05:00Z"

go deeper

for a junior

Recall that D1 Time Travel is always on, that the commands are wrangler d1 time-travel info and restore, and that a restore overwrites the database.

for a middle

Explain how a timestamp maps to a bookmark, why --timestamp and --bookmark are exclusive, what happens to in-flight queries, and how the printed previous bookmark undoes a restore.

for a senior

Plan the recovery so later good writes are not lost: export first, restore, replay; note the 10-restores-per-10-minutes limit and record bookmarks before risky changes.

for a principal

Decide what Time Travel's 30-day window covers and what needs exported archives, and how a whole-database restore shapes the way data is split across databases.

## What Time Travel is **Time Travel** is D1's point-in-time recovery. It needs no setup and no schedule: - It is **always on** and costs nothing extra; D1 records **bookmarks** (identifiers of database versions) automatically. - Retention is **30 days on Workers Paid** and **7 days on Free**; a bookmark older than 30 days is invalid as a restore point. - It works on databases using D1's production storage backend. `wrangler d1 info <db>` shows `version: production` for those; legacy alpha databases had snapshot backups instead. - Wrangler 4 removed the old `wrangler d1 backup` commands, which only served alpha databases. Restoring now means `wrangler d1 time-travel`, and copying data out means `wrangler d1 export`. ## Recovering from the bad UPDATE Suppose an `UPDATE products SET price_cents = 0` ran on the production catalogue at about 14:07 UTC. 1. Pick a time just before the statement, such as `2026-10-10T14:05:00Z`. Time Travel accepts a Unix timestamp in seconds or an RFC3339 timestamp with its time zone. 2. Run `wrangler d1 time-travel info catalogue --timestamp=...`. It prints the bookmark that timestamp corresponds to, and the exact restore command. 3. Save the current state with `wrangler d1 export catalogue --remote --output=...`. The restore will discard every write after the chosen minute, including good orders placed since. 4. Run `wrangler d1 time-travel restore catalogue --timestamp=...` (or `--bookmark=...`) and confirm the prompt. With `--json` there is no prompt and the result prints as JSON, which suits scripts but removes that last check. 5. Note the previous bookmark the command prints. Restoring to it undoes the restore. 6. Replay any rows worth keeping from the export. The command acts on the remote database only; the local database that `wrangler dev` uses has no Time Travel. ## Choosing the point to restore to Picking the minute is the step most likely to go wrong: - Find when the bad statement ran from the Worker's logs, the deploy or migration time, or the person who ran it. Time Travel itself does not tell you which statement changed what. - Work in UTC. A Unix timestamp is always UTC, and `date +%s` prints the current one; an RFC3339 timestamp must carry its offset, such as `Z` or `-04:00`. - Choose a minute safely **before** the statement. Restoring to a minute after it brings the damage back. - Check the choice with `time-travel info` first. The conversion from timestamp to bookmark is deterministic, so the bookmark it prints is exactly what `restore` will use. Bookmarks are the same identifiers the Sessions API uses for read replication, and they sort lexicographically from oldest to newest, so two bookmarks can be compared to see which state is later. ## What a restore does - It **overwrites the whole database in place**. There is no per-table restore, and Time Travel cannot yet fork the history into a new database. - **In-flight queries and transactions are cancelled**, and their callers receive an error. - It returns the **previous bookmark**, and restoring does not delete older bookmarks, so you can move to an earlier or later point again. - A timestamp converts to a bookmark **deterministically**: the same timestamp always means the same bookmark. - `--timestamp` and `--bookmark` are mutually exclusive, and one of them is required; Wrangler rejects both or neither before calling the API. ## Limits at a glance | Limit | Value | |---|---| | History kept | 30 days (Workers Paid), 7 days (Free) | | Restore operations | 10 per 10 minutes, per database | | Scope of a restore | the whole database | | Forking into a new database | not available yet | ## Beyond the 30 days Time Travel is not an archive. To keep a state longer, or to keep a copy outside the database, export it: `wrangler d1 export --remote` from a script, or a scheduled Workflow that exports through the REST API into R2. An export is also what lets you pick individual rows back, because a restore cannot. ## Habits that pay off - Record the current bookmark (`wrangler d1 time-travel info <db>`) before a risky migration or a manual data fix, so the restore target is exact. - Rehearse the restore on a non-production database; the confirmation prompt is the last chance to stop. - Remember that a restore of a database that serves several teams or tenants rewinds all of them.

  • The restore went to the wrong minute. How do you get back?
    Restore again. The first restore printed the previous bookmark, which is the state just before it ran; `wrangler d1 time-travel restore <db> --bookmark=<that id>` returns there. Restoring does not delete older bookmarks, so you can also pick another timestamp. Mind the limit of 10 restores per 10 minutes per database.
  • Why export before restoring if Time Travel already holds the history?
    Because a restore replaces the whole database, not one table. Orders, sign-ups or edits made after the chosen minute vanish with the bad UPDATE. An export taken first keeps them, so you can replay the rows worth keeping once the restore is done.

  • You must enable Time Travel or schedule backups before it can restore anything.
  • A Time Travel restore can roll back a single table and leave the rest untouched.
  • Restore with wrangler d1 backup restore, as in Wrangler 3.
  • Time Travel keeps history for 30 days on every plan.
  • A restore can never be undone once it finishes.

Assumes Wrangler 4 and D1 as of 2026-10-10: Time Travel keeps 30 days on Workers Paid and 7 on Free, and Wrangler 4 removed the alpha-only wrangler d1 backup commands.