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

10 replies
Last updated: Aug 11, 2023
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
Not sure if I fully understand your issue. If you created a document with the older schema and then removed the field(s) in the new schema, when you open it in the studio, you should see the error
Unknown field found
like in the attached screenshot.
Unfortunately I do not, I made slight changes to one schema that had data. The data wasn't showing anymore so I thought it was deleted. When I run the frontend, the query is showing old data that is not showing on the studio. When I try to visit the page, it can't be generated, same with the new schema. I am going to try to delete all the data from the CLI for just that schema. Having trouble with that too, when I run documents query to check the IDs to delete, it comes back as an empty array when it definitely has data in it. I'll try figure it out.
By data, do you mean the entire document or just certain fields? Can you see if you can pull that data using groq in the vision plugin ?
*[_type == 'your-schema-name-here']{
  _id,
  ...,
}
If you are able to get back the ID, then you should be able to delete the data using
the client .
Fixed the problem, very sorry. I changed the schema name from projects to project and understand now how to properly use the CLI and vision to not make that mistake again. That was a lot of hours, thank you for the replies! Highly and heavily appreciated!
happy to help :) glad that your issue is sorted!
When I changed the schema name from projects to project, did it delete the schema? Or is it still there? Yeah they're, tried that in vision, so meaning that schema still exists. Would I need to change the name back, delete the contents, then change the schema name again? Cause the data is there but the studio does not have the schema for me to view or manipulate.
You can perhaps grab the
_id
of the documents using groq in Vision and then delete them using the API like I mentioned earlier ?

... data is there but the studio does not have the schema for me to view or manipulate.
I wonder if you can even access them via the URL in your Studio and delete them from there, something like:
<http://localhost:3333/desk/projects;id-of-the-document-here>
Erm.. does not look like you can do this URL trick unfortunately.
No you can't, excuse, constantly thinking what to do and how to do it. Many ways to fix the problem, and the thought of the best option is exhausting me. Thank you so much for your time, you're awesome! Would it be easy to migrate the data inside of projects to project? Even if I did that, there're so many components querying using that "s". I'll do it anyways, whether I can transfer the data or not.
This may be another option you could potentially explore where you export-&gt;reshape-&gt;import your data.
I'll read through it, thank you. Learned a lot.

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?