How to delete all data for specific schema documents in Sanity?
I understand your situation - you've changed your schema significantly and now can't see the old data in Studio. The good news is that the data isn't lost, it's just orphaned because the schema definition no longer matches. You can definitely delete all documents of a specific type.
Here's the recommended approach using Sanity CLI for bulk deletion:
Basic Setup:
First, make sure you have the necessary tools installed:
npm install -g @sanity/cli
npm install -g groq-cliDelete documents by type:
You can delete all documents of a specific type with this command (split across lines for readability):
sanity documents query "*[_type == 'yourDocumentType'][0...20]._id" --apiVersion 2021-03-25 \
| groq "*" -o ndjson \
| xargs sanity documents deleteReplace 'yourDocumentType' with the actual _type of the documents you want to delete.
Important notes:
The query is limited to 20 documents to avoid timeouts. You'll need to run this multiple times until all documents are deleted.
For larger deletions (hundreds of documents), you can use a more efficient approach that makes a single API call instead of one per document. Check out the guide's section on "Delete faster, without timeouts."
Be careful - this permanently deletes documents! Make sure you're targeting the correct
_type.
Alternative approach:
If you prefer, you can also write a migration script using the Sanity client that gives you more control over the deletion process.
Before deleting, you might want to export your data first as a backup:
sanity dataset export production backup.tar.gzThis way, if you realize you needed some of that data, you can still recover it.
Show original thread10 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.