Deleted a document in Sanity? How to recover content and prevent it
By Nayan Kyada · · 6 min read
Part of The Sanity + Next.js Guide
Someone on your team deleted a document in Sanity, the page is now a 404 or a blank hole in your site, and nobody is sure whether it can come back. Stay calm and do not make more changes to the dataset until you have checked three places: document history, any backups, and your own exports. This post walks through that order, then the guardrails that stop it happening again.
First: stop the bleeding
Before you try to restore anything, work out what the deletion actually broke. A deleted document in Sanity does not only vanish from the Studio. Anything that references it is now pointing at nothing, and on a Next.js site that usually means one of three symptoms:
- The page itself returns a 404 because the slug no longer resolves.
- Other pages that referenced it (authors, categories, related posts) render with missing data or throw.
- Navigation or sitemap entries disappear after the next revalidation.
If pages are throwing errors, the fastest temporary fix is on the frontend: make the GROQ projection tolerate a missing reference, so the rest of the site stays up while you restore. I covered the failure mode in Sanity broken references crashing Next.js pages. Do that first if production is visibly broken, then recover the content.
Also ask the editor exactly what they deleted: a draft, a published document, or both. Deleting in the Studio removes the published version and its draft together, which surprises people who thought they were only discarding changes.
Where a deleted document in Sanity can come from
Check these in order. Each one is quicker than the next.
- Document history in the Studio. Sanity keeps a revision history for documents, and how far back it goes depends on your plan. Confirm the retention window in your project settings rather than assuming. If the deletion was recent, this is the first place to look, and if you can reach the document's history you can often restore a previous revision.
- Sanity-managed backups. Some plans include dataset backups. Check the project's settings in the Sanity management console to see whether any exist and how old they are.
- Your own exports. If anyone ever ran a dataset export (for a migration, a staging copy, or a handover), that file is a snapshot you can pull the document out of.
- A staging dataset. If you copied production to a staging dataset at some point, the document may still live there.
If none of those exist, the content is likely gone from Sanity. Your remaining options are the live site's cached HTML, a search engine cache, or the Wayback Machine, which can at least give you the text and structure to retype.
Restoring from an export
If you have an export file, do not import it over production. Import it into a scratch dataset, find the document, and copy only that record across. This avoids overwriting everyone else's work since the export was taken.
# Create a scratch dataset and load the old export into it
npx sanity dataset create restore-scratch
npx sanity dataset import backup-2026-10-01.tar.gz restore-scratchThen query the scratch dataset for the missing document by its slug or title. You can run a quick GROQ query from the Vision tool pointed at that dataset:
*[_type == "post" && slug.current == "the-deleted-slug"][0]Once you have the JSON, create it in production with the same _id so existing references resolve again. Keeping the original _id is the important part. A new ID means every document that referenced the old one is still broken. A short script with the Sanity client does it:
// scripts/restore-document.ts
import {createClient} from '@sanity/client'
import restored from './restored-post.json'
const client = createClient({
projectId: process.env.SANITY_PROJECT_ID!,
dataset: 'production',
apiVersion: '2026-01-01',
token: process.env.SANITY_WRITE_TOKEN!,
useCdn: false,
})
async function main() {
// createIfNotExists keeps us from clobbering anything that was recreated meanwhile
const result = await client.createIfNotExists(restored)
console.log('Restored', result._id)
}
main()Images and files are stored as separate asset documents. If the old export included assets, the references usually still resolve as long as the asset documents still exist in production. If the assets were also deleted, you will need to re-upload them.
After restoring, trigger a revalidation (publish nothing, just fire your webhook or revalidate the tag manually) so the Next.js site picks the document up again. If published changes are not appearing, see Sanity editors publishing changes that don't show on the live site.
Stop it happening again
Recovery is a stressful afternoon. Prevention takes an hour. Here is what I put in place on sites where content is business-critical.
Remove the delete action where it is risky
Editors rarely need to hard-delete a landing page, a service page, or a settings document. Remove the action for those types in the Studio config:
// sanity.config.ts
import {defineConfig} from 'sanity'
const protectedTypes = ['page', 'siteSettings', 'navigation']
export default defineConfig({
// ...projectId, dataset, plugins, schema
document: {
actions: (prev, {schemaType}) =>
protectedTypes.includes(schemaType)
? prev.filter((action) => action.action !== 'delete')
: prev,
},
})This is a UI guard, not a security boundary. Someone with the right permissions can still delete through the API, so combine it with roles. I wrote up that side in Sanity roles and document-level permissions.
Give editors a safe alternative
People delete because they have no other way to say "take this down". An unpublish and archive workflow solves that, and the content stays recoverable. The approach is in how to archive documents in Sanity.
Keep your own scheduled export
Do not depend on whatever your plan happens to retain. A scheduled export to storage you control is cheap insurance:
# Run nightly from CI (GitHub Actions, cron, etc.)
npx sanity dataset export production "backups/production-$(date +%F).tar.gz"Store the files somewhere that is not the same account as the project, keep a sensible number of them, and test a restore into a scratch dataset once. An untested backup is a guess.
Put it in the handover
Backups, token ownership, and who holds admin access should be written down, not living in one person's head. That is part of what I list in the Sanity project handover checklist.
What to ask a developer you bring in
- What exactly is recoverable today, and how do you know?
- Will you restore with the original
_idso references stay intact? - Will you restore into a scratch dataset first, not production?
- What will be in place afterwards: delete guards, roles, scheduled exports?
- Can you show me a restore test, not just describe one?
If the answers are vague, the next deletion will be just as painful.
If you need a hand
Recovering lost content, tightening permissions, and setting up exports is work I do as part of a Performance & Sanity audit, which starts from $800. Details are on the services page.
Frequently asked questions
01Can you recover a deleted document in Sanity?
Often yes, but it depends on what you have. Document history in the Studio may help depending on your plan's retention window, and a dataset export or backup lets you restore the document by importing it. If you have neither, recovery gets much harder, so check both before anything else.
02Does Sanity back up my dataset automatically?
Not in a way you should rely on blindly. Backup features and history retention vary by plan, so confirm what your plan includes in your project settings and on Sanity's pricing page. Keeping your own scheduled export is the safe option.
03How do I stop editors from deleting Sanity documents?
Remove the delete action for important document types in your Studio config, and restrict delete permissions with roles. Pair that with an archive or unpublish workflow so editors still have a safe way to take content down.