How to delete document causing desk crashes after schema change?

29 replies
Last updated: May 26, 2021
Hi! I changed the schema for a document and now my old document crashes the desk when I try to edit it. Is there a good way for me to delete the document, or figure out its id to delete it from the command line?
AI Update

Oof, that's frustrating when the desk crashes and you can't even access the document to delete it! I've been there. Here are a few ways to handle this:

Finding the document ID

If you can see the document in your Studio's list view (even if clicking it crashes), you can usually hover over or right-click it to see the document ID in the URL or browser status bar. The ID will look something like drafts.abc123-def4-5678-90gh-ijklmnopqrst.

Alternatively, you can use the CLI to query for documents:

sanity documents query "*[_type == 'yourDocumentType']._id"

This will list all document IDs of that type. You can add more filters to narrow it down if needed.

Deleting the document

Once you have the ID, you can delete it directly from the CLI:

sanity documents delete YOUR-DOCUMENT-ID

If you need to delete multiple problematic documents, there's a helpful guide on bulk deletion using the CLI. For example, to delete all documents of a specific type:

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

Note: The guide limits this to 20 documents at a time to avoid timeouts. You can run it multiple times if needed.

Pro tip for the future

Before making schema changes, it's a good idea to export your dataset as a backup:

sanity dataset export production backup.tar.gz

This way, if something goes wrong with a schema change, you can investigate the data structure or restore if needed. The schema migration docs also have some good practices for handling schema changes safely.

Hope this gets you unstuck! Let me know if the document delete command gives you any trouble.

Show original thread
29 replies
You can delete it using the Sanity client library - https://www.sanity.io/docs/js-client (there is also PHP and .NET)
Example from that page,
const sanityClient = require('@sanity/client')
const client = sanityClient({
  projectId: 'your-project-id',
  dataset: 'bikeshop',
  apiVersion: '2019-01-29', // use current UTC date - see "specifying API version"!
  token: 'sanity-auth-token', // or leave blank for unauthenticated usage
  useCdn: true, // `false` if you want to ensure fresh data
})

// ^^^ Sanity client setup

client
  .delete('bike-123') // The ID
  .then((res) => {
    console.log('Bike deleted')
  })
  .catch((err) => {
    console.error('Delete failed: ', err.message)
  })
Sorry, I misread the question. You should be able to get the ID from inside the studio, it’ll be the end part of the url
Yeah, I tried that (was so excited to think of it!). But it didn't work, and I checked and none of my other documents have their ids in the url; is that because I'm on localhost 3333, maybe? I'll check the deployed version, brb
Same problem (and same url) on sanity.studio
The url that crashes it:
https://ceramoni-sanity.sanity.studio/desk/item;e0c81df9-6632-4e4e-9fdf-ac4d50e64343
When I run
sanity documents delete e0c81df9-6632-4e4e-9fdf-ac4d50e64343
I get
Document not found

I checked another, non-problematic, document on graphQL (the problematic document is not showing up on graphQL) and the id was different from the url. For
http://localhost:3333/desk/stone;0a9f4d12-6465-460d-ab1f-089c771ed916 graphQL shows the id as -93fd7a92-6174-5152-beff-cea6e0451008
Oh interesting
What if you did a query on the document type using a property that is only in the old schema?
*[_type == "type" && defined(property)]{ … }
Well, right now I've only got one document of the problematic type. And when I go to that field in graphQL (allSanityItem), it's not returning any nodes
*[_type == "type" && defined(property)]{ … }
OK, I'm gonna have to learn a little to do this. Sorry! I'm very new. That looks like something to put into the javascript client?
Well, right now I've only got one document of the problematic type. And when I go to that field in graphQL (allSanityItem), it's not returning any nodes
If you have the Vision plugin on the local studio, you can run the groq query in there:
Same problem (and same url) on sanity.studio
The url that crashes it:
https://ceramoni-sanity.sanity.studio/desk/item;e0c81df9-6632-4e4e-9fdf-ac4d50e64343
When I run
sanity documents delete e0c81df9-6632-4e4e-9fdf-ac4d50e64343
I get
Document not found

I checked another, non-problematic, document on graphQL (the problematic document is not showing up on graphQL) and the id was different from the url. For
http://localhost:3333/desk/stone;0a9f4d12-6465-460d-ab1f-089c771ed916 graphQL shows the id as -93fd7a92-6174-5152-beff-cea6e0451008
I think you could even just paste
*[defined(allSanityItem)]
and it should return any document with that property defined
then you can grab the
_id
from the result
Heard, thanks!
Oh interesting
What if you did a query on the document type using a property that is only in the old schema?
"No documents found in dataset 
production
 that match query:
*[defined(allSanityItem)]
"
Thanks Racheal!
I believe if you query
*
it will return all of the documents in your dataset
👍 trying that now
That did it!
The problem was that "drafts" was prepended onto the url id
so
sanity documents delete drafts.e0c81df9-6632-4e4e-9fdf-ac4d50e64343
worked where it hadn't worked without the "drafts." on the front of the id
Thanks y'all! I really appreciate such fast and expert help. Also, the docs have been great to work with: I'm new (can you tell?) and I've found them to be much more accessible than most. Y'all have a great day!
Nice! Glad you got it sorted! And also glad to help you along you Sanity learning journey!
☺️

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?