How to restore accidentally deleted documents from Sanity dataset
I'm really sorry to hear you accidentally wiped your products! The good news is that yes, you can restore deleted documents in Sanity if they were deleted within your plan's history retention period.
Quick Recovery Options
1. Restore via Studio UI (Easiest)
If you still have the document IDs, you can restore individual documents directly through the Studio:
- Navigate to
https://YOUR-PROJECT.sanity.studio/YOUR-DATASET/YOUR-DOC-TYPE/YOUR-DOC-ID - Even though the document is deleted, you can access its history and restore it using the history interface
2. Use the History API (For Bulk Restoration)
For restoring multiple products programmatically, you'll need to use the History API. Here's the approach based on a successful community recovery:
Step 1: Get the transaction ID of the deletion event
GET https://YOUR-PROJECT.api.sanity.io/v1/data/history/YOUR-DATASET/transactions
Step 2: Find the documents that were deleted in that transaction
GET https://YOUR-PROJECT.api.sanity.io/v1/data/history/YOUR-DATASET/transactions/TRANSACTION-ID?excludeContent=true&reverse=true
Step 3: For each deleted document, get its last revision before the deletion. The key insight is that you need to query the transaction before the deletion transaction to get the actual document content.
Step 4: Use the JavaScript client's createOrReplace() method to restore each document with the retrieved content.
A community member successfully restored 400 accidentally deleted documents using this exact approach. The tricky part is that querying the deletion transaction itself returns empty documents, so you need to get the previous transaction for each document.
Important Caveats
History retention depends on your plan (as detailed in the history documentation):
- Free: 3 days
- Growth: 90 days
- Enterprise: 365 days (customizable)
If your deletion happened outside your retention window, the history is permanently gone.
Prevention for Next Time
Before making bulk changes, always export your dataset as a backup using the CLI export command:
sanity dataset export production backup-before-changes.tar.gzYou can then restore if something goes wrong:
sanity dataset import backup-before-changes.tar.gz productionConsider setting up automated backups using GitHub Actions or scheduled scripts, especially for production environments, since Sanity doesn't provide built-in backup storage.
Good luck with the recovery! If you're within your retention period, you should be able to get everything back.
Show original thread32 replies
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.