Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

Troubleshooting document deletion and finding document ID in Sanity

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?
May 26, 2021, 3:54 PM
You can delete it using the Sanity client library - https://www.sanity.io/docs/js-client (there is also PHP and .NET)
May 26, 2021, 4:16 PM
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)
  })
May 26, 2021, 4:16 PM
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
May 26, 2021, 4:19 PM
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
May 26, 2021, 4:20 PM
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
May 26, 2021, 4:25 PM
Oh interesting
May 26, 2021, 4:27 PM
What if you did a query on the document type using a property that is only in the old schema?
May 26, 2021, 4:27 PM
*[_type == "type" && defined(property)]{ … }
May 26, 2021, 4:28 PM
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
May 26, 2021, 4:28 PM
*[_type == "type" && defined(property)]{ … }
May 26, 2021, 4:28 PM
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?
May 26, 2021, 4:30 PM
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
May 26, 2021, 4:28 PM
If you have the Vision plugin on the local studio, you can run the groq query in there:
May 26, 2021, 4:31 PM
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
May 26, 2021, 4:25 PM
I think you could even just paste
*[defined(allSanityItem)]
and it should return any document with that property defined
May 26, 2021, 4:32 PM
then you can grab the
_id
from the result
May 26, 2021, 4:32 PM
Heard, thanks!
May 26, 2021, 4:33 PM
Oh interesting
May 26, 2021, 4:27 PM
What if you did a query on the document type using a property that is only in the old schema?
May 26, 2021, 4:27 PM
"No documents found in dataset 
production
 that match query:
*[defined(allSanityItem)]
"
May 26, 2021, 4:34 PM
Thanks Racheal!
May 26, 2021, 4:34 PM
I believe if you query
*
it will return all of the documents in your dataset
May 26, 2021, 4:35 PM
👍 trying that now
May 26, 2021, 4:35 PM
That did it!
May 26, 2021, 4:37 PM
The problem was that "drafts" was prepended onto the url id
May 26, 2021, 4:37 PM
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
May 26, 2021, 4:39 PM
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!
May 26, 2021, 4:40 PM
Nice! Glad you got it sorted! And also glad to help you along you Sanity learning journey!
May 26, 2021, 4:41 PM
☺️
May 26, 2021, 4:41 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?