Deleting content after accidentally deleting the schema first
Good news - you can still delete those documents even though you removed the schema! Since Sanity stores your content independently from your schema definitions, the data persists in your Content Lake even after you delete the schema code.
Here are your best options:
Using Vision Plugin (Easiest)
The simplest approach is to use the Vision plugin in your Sanity Studio:
- Open your Studio and go to the Vision tab
- Write a GROQ query to find your documents:
*[_type == "yourSchemaType"] - You can see all the documents that still exist
- Get the document IDs to delete them via CLI or script
Using the Sanity CLI
You can delete documents directly using the Sanity CLI with the sanity documents delete command:
# Delete a single document by ID
sanity documents delete <document-id>
# Query and delete multiple documents
sanity documents query '*[_type == "yourSchemaType"]' | jq -r '.[].\_id' | xargs -I {} sanity documents delete {}Using a Script with the Sanity Client
For bulk deletions (which sounds like your situation), you can write a Node.js script using @sanity/client:
import {createClient} from '@sanity/client'
const client = createClient({
projectId: 'your-project-id',
dataset: 'your-dataset',
token: 'your-token-with-write-access',
apiVersion: '2024-01-01',
useCdn: false
})
async function deleteDocuments() {
const query = '*[_type == "yourSchemaType"][0...100]'
const documents = await client.fetch(query)
if (documents.length === 0) {
console.log('No more documents to delete')
return
}
const transaction = client.transaction()
documents.forEach(doc => transaction.delete(doc._id))
await transaction.commit()
console.log(`Deleted ${documents.length} documents`)
// Run again if there are more
if (documents.length === 100) {
await deleteDocuments()
}
}
deleteDocuments()As mentioned in this community thread, you may need to run this in batches with cooldowns to avoid rate limiting. Process 100-200 documents at a time, and if you have thousands of documents, you might need to add a small delay between batches.
Alternative: Export and Reimport
If you have other content you want to keep, you could also export your dataset without those documents and work from that clean dataset instead. This approach was suggested in the community thread as well - export everything except the documents you want to remove, then work from that dataset.
The key thing to remember is that in Sanity, your schema is just a validation layer - the actual content lives independently in the Content Lake, which is why you can still access and delete it even after removing the schema definition. Your data will continue to be returned by queries until you explicitly delete it.
Show original thread5 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.