How to delete documents from removed schemas or find dangling data in Sanity?

5 replies
Last updated: Jun 14, 2021
Hey, I decided to do some Sanity spring cleaning and removed some schemas before removing all the documents. Now I have a document that can't delete. I tried reverting to an old commit, starting up Sanity locally, but that lead to a different error.
Ideally, I would be able to see all my data that is dangling, without any references, and delete them?
AI Update

Ah, the classic "deleted the schema before the documents" scenario! Don't worry, this is a common issue and there's a good solution using the Sanity CLI.

Since you removed the schema, those documents are now invisible in Studio, but they still exist in your dataset. Here's how to handle this:

The Solution: Use CLI to Delete Documents

You can delete documents without a schema using the sanity documents delete command. The key is querying for documents by their _type and then deleting them.

Basic Command Structure

sanity documents query "*[_type == 'yourDocumentType'][0...20]._id" --apiVersion 2021-03-25 
  | groq "*" -o ndjson 
  | xargs sanity documents delete

Important notes:

  • Replace 'yourDocumentType' with the actual type of the documents you want to delete
  • The [0...20] limits the query to 20 documents at a time to avoid timeouts
  • You'll need to run this multiple times if you have more than 20 documents

Finding All Dangling Documents

To see all documents that might be dangling (without any references), you can query for documents with a specific type, or if you're not sure of the type, you can:

  1. Export your dataset to see what's there:
sanity dataset export
  1. Query for all document types:
sanity documents query "*[]._type" --apiVersion 2021-03-25

This will show you all the document types in your dataset, helping you identify the orphaned ones.

For Faster Bulk Deletion

If you have many documents to delete (50+), the command above can timeout. For better performance, you can use a tool like Console.Join to batch the deletions into a single API call:

sanity documents query "*[_type == 'yourDocumentType']._id" --apiVersion 2021-03-25 
  | groq "*" -o ndjson 
  | join 
  | xargs sanity documents delete

Alternative: Use a Migration Script

For more complex cleanup operations, you might want to use Sanity's migration tooling. Create a migration script with:

npx sanity migration create deleteOrphaned

This gives you more control and better error handling, especially useful if you need to delete documents based on more complex criteria than just type.

The CLI approach is definitely the way to go here - reverting commits won't help since the documents still exist in your dataset regardless of your local schema definitions.

Show original thread
5 replies
If so, you can take the
_id
from the URL (if your URL is
<http://localhost:3333/desk/post;031061f3-0d34-4bg2-a34c-92f1a53e5dde>
, the
_id
is
031061f3-0d34-4bg2-a34c-92f1a53e5dde
) and then run this command in your terminal:

sanity documents delete "031061f3-0d34-4bg2-a34c-92f1a53e5dde"
You might also need to run this command if there’s a draft kicking around:


sanity documents delete "drafts.031061f3-0d34-4bg2-a34c-92f1a53e5dde"
If so, you can take the
_id
from the URL (if your URL is
<http://localhost:3333/desk/post;031061f3-0d34-4bg2-a34c-92f1a53e5dde>
, the
_id
is
031061f3-0d34-4bg2-a34c-92f1a53e5dde
) and then run this command in your terminal:

sanity documents delete "031061f3-0d34-4bg2-a34c-92f1a53e5dde"
You might also need to run this command if there’s a draft kicking around:


sanity documents delete "drafts.031061f3-0d34-4bg2-a34c-92f1a53e5dde"
user A
thanks, that fixed it!
Great! 🙌
Loving Sanity, great product!

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?