Deleting bulk documents in Sanity Studio using CLI or Vision
There are two officially documented ways to bulk delete documents from Sanity:
1. Using the Sanity CLI (Recommended for small batches)
The official CLI documents command provides a delete subcommand that can delete documents by ID:
# Delete single document
sanity documents delete myDocId
# Delete multiple documents by ID
sanity documents delete doc1 doc2 doc3Important limitation: The CLI delete command works by document ID only. There's no built-in --query flag to delete by filter directly from the CLI.
2. Using @sanity/client with a query (For larger batches)
For deleting documents based on a GROQ query, you need to use the JavaScript client. There's an official recipe for this approach:
/**
* THIS SCRIPT DELETES DATA!
*
* To use:
* 1. Save this as deleteDocsByFilter.js in your studio folder
* 2. Backup your dataset: sanity dataset export
* 3. Run: sanity exec deleteDocsByFilter.js --with-user-token
*/
import client from 'part:@sanity/base/client'
client
.delete({query: '*[_type == "aDocumentType"][0...999]'})
.then(console.log)
.catch(console.error)Important notes:
- ⚠️ The
[0...999]slice is critical - you can delete up to ~1000 documents per transaction - Always export your dataset before bulk deletions
- Test your GROQ query first using Vision plugin or
sanity documents query
About Vision Plugin
The Vision plugin is great for testing queries, but it cannot perform deletions. Vision is read-only for querying - you must use the CLI or client for mutations.
Testing Your Query First
Before deleting, verify your query targets the right documents:
# Preview which documents will be affected
sanity documents query '*[_type == "myType"]._id'This approach is documented in the official Sanity HTTP mutations reference, which confirms the delete mutation accepts a query parameter for bulk operations.
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.