How to delete all data for specific schema documents in Sanity?

10 replies
Last updated: Nov 29, 2025
Hello! I had data in my schema, changed the schema up to the point where it seems to have turned into a new schema and now I can not see the data anymore. Is there a way to delete only all data for specific schema documents?
AI Update

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-cli

Delete 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 delete

Replace 'yourDocumentType' with the actual _type of the documents you want to delete.

Important notes:

  1. The query is limited to 20 documents to avoid timeouts. You'll need to run this multiple times until all documents are deleted.

  2. 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."

  3. 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.gz

This way, if you realize you needed some of that data, you can still recover it.

Show original thread
10 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.

Was this answer helpful?