šŸ”® Sanity Create is here. Writing is reinvented. Try now, no developer setup

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.
Jun 25, 2020, 2:34 PM
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.
Jun 25, 2020, 2:54 PM
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.
Jun 25, 2020, 3:00 PM
user M
This is pretty urgent, any help here?
Jun 25, 2020, 3:22 PM
Hi both, that sounds worrying, sorry to hear. Could you DM me your project ID?
Jun 25, 2020, 3:23 PM
Hi both, that sounds worrying, sorry to hear. Could you DM me your project ID?
Jun 25, 2020, 3:23 PM
user M
This is pretty urgent, any help here?
Jun 25, 2020, 3:22 PM
Also, just in case, could you share the transaction ID?
Jun 25, 2020, 3:29 PM
The one that we accidentally deleted all the products correct?
Jun 25, 2020, 3:30 PM
You want it in here or DM?
Jun 25, 2020, 3:31 PM
DM is fine šŸ™‚
Jun 25, 2020, 3:31 PM
Are you seeing the relevant transaction in the newly created documentsā€™ history btw (considering they have the same IDs as the old docs)?
Jun 25, 2020, 3:35 PM
Yes, we are. We can see history for the documents that exist now.
Jun 25, 2020, 3:35 PM
We were going to try reverting a couple individual documents via the studio UI and reverse-engineer the request it sends.
Jun 25, 2020, 3:37 PM
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.
Jun 25, 2020, 3:39 PM
okay thank you so much šŸ˜„
Jun 25, 2020, 3:40 PM
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?
Jun 25, 2020, 3:47 PM
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.
Jun 25, 2020, 3:49 PM
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.
Jun 25, 2020, 3:50 PM
[It shouldnā€™t] šŸ‘
Jun 25, 2020, 3:51 PM
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.
Jun 25, 2020, 3:59 PM
So I tried that, and used the exact time stamp of the deletion and it doesnā€™t seem to return the data we need
Jun 25, 2020, 4:00 PM
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": []
}
Jun 25, 2020, 4:02 PM
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 šŸ™‚
Jun 25, 2020, 4:06 PM
thatā€™s fine! Thank you!
Jun 25, 2020, 4:06 PM
Is it because the documents that were deleted didn't exist as of the transaction that deleted them?
Jun 25, 2020, 4:13 PM
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).
Jun 25, 2020, 4:15 PM
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.
Jun 25, 2020, 5:17 PM
Yep, this is exactly what I am in the process of doing.
Jun 25, 2020, 5:19 PM
Appreciate all the help! It's good to have confirmation that this approach is the correct way.
Jun 25, 2020, 5:20 PM
Haha thank you though Peter! We will keep you updated!
Jun 25, 2020, 5:20 PM
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!
Jun 25, 2020, 5:31 PM
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.
Jun 25, 2020, 5:55 PM
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 šŸ™‚
Jun 25, 2020, 9:19 PM
That'd be great! Thanks again for the help today!
Jun 25, 2020, 9:20 PM

Sanityā€“ build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?