Restoring accidentally deleted documents in Sanity dataset without exporting.

34 replies
Last updated: Jun 25, 2020
Is there a way to restore docs from a dataset in sanity, I accidentally wiped all my products.
AI Update

I'm really sorry to hear about your products getting wiped! The good news is that yes, you can likely restore them using Sanity's document history system, as long as the deletion happened within your plan's retention period.

Quick Recovery Steps

The fastest way to restore your deleted products is through the History API. Here's what you need to know:

First, check your retention window:

  • Free plan: 3 days
  • Growth plan: 90 days
  • Enterprise plan: 365 days

If your deletion happened within this window, your data is still recoverable!

How to Restore Deleted Documents

Even though documents are deleted, you can still access them if you know their _id. You have two main options:

Option 1: Via Studio URL (Manual)

  1. Navigate directly to the document using its ID in your Studio URL: https://your-studio.sanity.studio/desk/yourDocumentType;documentId
  2. Once there, you can view the document history and restore it

Use the History API to retrieve previous versions of your deleted products, then restore them using the JavaScript client's createOrReplace() method with the entire document from the history API as the payload.

Here's a basic approach:

  1. Query the History API to get the previous state of your products
  2. Use the _rev property to identify the revision you want to restore
  3. Use client.createOrReplace() with the retrieved document data

You can find detailed guidance on this process in the restoring deleted documents guide and these community answers about restoring accidentally deleted documents.

Important Notes

  • The latest version of both published and draft documents always remains available, even after the retention period
  • History beyond your retention period is permanently deleted, so act quickly
  • Sanity stores all document patches as transactions in the Content Lake, creating a complete revision history that tracks who made changes and when

If you're on a Free plan and it's been more than 3 days, or if you need help with the restoration process, I'd recommend reaching out to Sanity support directly—they may be able to help depending on your situation.

Show original thread
34 replies
We're desperately needing help here. We accidentally deleted around 400 documents and re-created new ones with the same IDs. Unfortunately, we did not export the dataset in-between.
We're needing to restore the old versions of all these documents but can't quite piece together how to accomplish this from the docs on the History API. We can get the transactions, but that only takes us so far (especially considering we can't get the old content due to
excludeContent: true
being required.
Update: It seems like we have an ID of the transaction where all of these were deleted. What we can't figure out is how to "undo" that transaction.
user M
This is pretty urgent, any help here?
Hi both, that sounds worrying, sorry to hear. Could you DM me your project ID?
Hi both, that sounds worrying, sorry to hear. Could you DM me your project ID?
user M
This is pretty urgent, any help here?
Also, just in case, could you share the transaction ID?
The one that we accidentally deleted all the products correct?
You want it in here or DM?
DM is fine šŸ™‚
Are you seeing the relevant transaction in the newly created documents’ history btw (considering they have the same IDs as the old docs)?
Yes, we are. We can see history for the documents that exist now.
We were going to try reverting a couple individual documents via the studio UI and reverse-engineer the request it sends.
That’s creative šŸ˜‰ And one way to get things back I guess, if the former product IDs are matched 1:1 with the new ones. But would be good to have an undo for the transaction itself. I’ve asked the team but it might take a moment to investigate this.
okay thank you so much šŸ˜„
user M
Would you advise against doing what we were going to do for any reason? Should we wait until you've investigated before restoring any individual documents?
If you feel comfortable moving ahead that way, it’s likely to be faster in this case, so feel free to do so šŸ™‚ Just keep us posted!
I’ll let you know if I get an update on the transaction question in the meantime. We won’t take action without informing you.
Okay. If there's still a way to do some kind of mass-restore, we'd definitely still be interested. Just wanted to make sure restoring individual documents at this point wouldn't hinder that effort in any way.
[It shouldn’t] šŸ‘
Ok, so here’s some practical input šŸ™‚
Step 1: Get all the documents involved in this transaction usingĀ 
GET /transactions
Ā API.
Step 2: Use history API’sĀ 
GET /history/documents
Ā endpoint to build all the documents as they looked during that transaction.
Step 3: Submit Create mutation to recreate these documents.
So I tried that, and used the exact time stamp of the deletion and it doesn’t seem to return the data we need
For example:
<https://c3g4r239.api.sanity.io/v1/data/history/dev/documents/2-FMT-2?revision=IT2L0aqWToOorOcvdRoqsD>

This is the deletion transaction id

doing a get request for that just returns:

{
    "documents": []
}
That’s not a very useful response indeed. We’ll check but will take around half an hour until someone can look at it - in case you’re in a hurry šŸ™‚
that’s fine! Thank you!
Is it because the documents that were deleted didn't exist as of the transaction that deleted them?
So we need to somehow programmatically get the most recent transaction ID before that one for each document? (It could be different for each document since they were all edited at different points before the accidental mass-delete).
That took a bit longer than expected. There’s still no straightforward way but this should provide one method. Whether it’s easier than what you’re already carrying out with the individual docs depends:

<https://c3g4r239.api.sanity.io/v1/data/history/dev/transactions/2-FMT-2?excludeContent=true&reverse=true>

The above API call also returns all the docs involved in the delete transaction, loop through them to get all 400 doc ids. Call the same API to get one-before-delete transaction for the all the documents. CallĀ 
/history/documents
Ā API on all these documents with revision id (transaction id) to get the snapshot.
Yep, this is exactly what I am in the process of doing.
Appreciate all the help! It's good to have confirmation that this approach is the correct way.
Haha thank you though Peter! We will keep you updated!
That’s great, you found the way before we could show it - next time I’ll ask you instead šŸ˜„ Hope things will settle this way!
user M
Looks like we have got it working!
user K
ran his script and it seemed to revert all the docs to the prev transaction before the delete.
That’s awesome, well done! Must have taken some detective work to get everything lined up correctly for the script to run. We’ll work on improving the docs around recovery as you’re surely not the first to run into this šŸ™‚
That'd be great! Thanks again for the help today!

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?