Restoring accidentally deleted documents in Sanity dataset without exporting.
34 replies
Last updated: Jun 25, 2020
U
Is there a way to restore docs from a dataset in sanity, I accidentally wiped all my products.
Jun 25, 2020, 2:34 PM
K
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
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: truebeing required.
Jun 25, 2020, 2:54 PM
K
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
U
user M
This is pretty urgent, any help here?Jun 25, 2020, 3:22 PM
M
Hi both, that sounds worrying, sorry to hear. Could you DM me your project ID?
Jun 25, 2020, 3:23 PM
P
Hi both, that sounds worrying, sorry to hear. Could you DM me your project ID?
Jun 25, 2020, 3:23 PM
T
user M
This is pretty urgent, any help here?Jun 25, 2020, 3:22 PM
P
Also, just in case, could you share the transaction ID?
Jun 25, 2020, 3:29 PM
T
The one that we accidentally deleted all the products correct?
Jun 25, 2020, 3:30 PM
T
You want it in here or DM?
Jun 25, 2020, 3:31 PM
P
DM is fine š
Jun 25, 2020, 3:31 PM
P
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
M
Yes, we are. We can see history for the documents that exist now.
Jun 25, 2020, 3:35 PM
M
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
P
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
T
okay thank you so much š
Jun 25, 2020, 3:40 PM
M
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
P
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.
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
M
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
P
[It shouldnāt] š
Jun 25, 2020, 3:51 PM
P
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
T
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
T
For example:
This is the deletion transaction id
doing a get request for that just returns:
<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
P
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
T
thatās fine! Thank you!
Jun 25, 2020, 4:06 PM
M
Is it because the documents that were deleted didn't exist as of the transaction that deleted them?
Jun 25, 2020, 4:13 PM
M
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
P
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
M
Yep, this is exactly what I am in the process of doing.
Jun 25, 2020, 5:19 PM
M
Appreciate all the help! It's good to have confirmation that this approach is the correct way.
Jun 25, 2020, 5:20 PM
T
Haha thank you though Peter! We will keep you updated!
Jun 25, 2020, 5:20 PM
P
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
T
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
P
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
M
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.