How to restore entire dataset to previous date or undo recent changes?

6 replies
Last updated: Feb 2, 2023
Is there a way to restore an entire dataset to what it was on a previous date or undo the most recent change on all documents? Asking for a friend... 😬
AI Update

😬 I feel your friend's pain! Unfortunately, Sanity doesn't have a built-in way to do a full dataset rollback to a previous date or bulk-undo across all documents.

What Sanity DOES offer:

Document History lets you restore documents one at a time by viewing their revision history and rolling back to a previous version. You can access this through the document status indicator at the bottom of the editor or via the contextual menu (ellipsis icon) → "History".

Important limitations:

  • Retention periods vary by plan:
    • Free: 3 days
    • Growth: 90 days
    • Enterprise: 365 days (customizable)
  • After the retention period, older revisions are permanently deleted (though the latest published/draft versions remain)

For bulk restoration:

If your friend needs to restore multiple documents, they'd need to use the History API programmatically. You could:

  1. Query the history for each document
  2. Retrieve the revision from your target date (using the _rev property)
  3. Use the JavaScript client's createOrReplace() method to restore each document

This would essentially be a custom script to loop through documents and restore them individually. Not ideal, but it's the available path.

Prevention tip: For future peace of mind, consider exporting dataset backups regularly using the sanity dataset export CLI command. That way you have a safety net outside of the built-in history retention!

Show original thread
6 replies
Unfortunately, no, you can’t rollback an entire dataset. You could probably use the History API to programmatically revert documents to a previous
_rev
, though.
That’s what I was afraid of. Guess I shouldn’t have put off that ā€œbackup sanity to s3ā€ task for so long lol. Thanks for the tip on the history API!
user M
Should I be able to set a
_rev
using the JS client? Something like:

    {
      id: doc._id,
      patch: {
        set: { _rev: doc._rev },
      },
    }

More importantly: if I set an old revision would that restore images on the document?
I worked it out by watching the network tab in the studio. Thanks again for the tip on the history api, that worked wonders.
For anyone who happens to see this: you pull the version of the document you want via the history api, then you can use
.createOrUpdate()
from the js client with the entire document from the history api as the payload.
Glad you got it sorted out!

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?