How to delete multiple documents in Sanity?
There are several ways to delete multiple documents in Sanity, depending on your needs and where you're working:
Using the Sanity CLI (Recommended for bulk operations)
The Sanity CLI provides a bulk deletion command that's perfect for deleting many documents at once. Here's the basic syntax:
sanity documents query '*[_type == "myDocument"]' --apiVersion 2021-03-25 | groq "*" -o ndjson | xargs sanity documents deleteThis queries for documents of a specific type and pipes them to the delete command. You can use any GROQ query to filter which documents to delete:
# Delete all drafts
sanity documents query '*[_id in path("drafts.**")]' --apiVersion 2021-03-25 | groq "*" -o ndjson | xargs sanity documents delete
# Delete documents with specific conditions
sanity documents query '*[_type == "post" && publishedAt < "2023-01-01"]' --apiVersion 2021-03-25 | groq "*" -o ndjson | xargs sanity documents deleteTools needed:
- Sanity CLI:
npm install -g @sanity/cli - GROQ CLI:
npm install -g groq-cli - On Windows: Install FindUtils from GnuWin for the
xargscommand
Using the JavaScript Client
For programmatic deletion in your code, you can use the Sanity JavaScript client with transactions:
import {createClient} from '@sanity/client'
const client = createClient({
projectId: 'your-project-id',
dataset: 'your-dataset',
token: 'your-token', // needs write permissions
apiVersion: '2024-01-01',
useCdn: false
})
// Query for document IDs you want to delete
const docsToDelete = await client.fetch('*[_type == "myDocument"]._id')
// Create a transaction to delete them
const transaction = client.transaction()
docsToDelete.forEach(id => {
transaction.delete(id)
})
await transaction.commit()Using sanity exec
You can also create a script and run it with sanity exec:
// delete-docs.js
import {getCliClient} from 'sanity/cli'
const client = getCliClient()
const query = '*[_type == "myDocument"]'
const docs = await client.fetch(query)
for (const doc of docs) {
await client.delete(doc._id)
console.log(`Deleted ${doc._id}`)
}Then run it:
sanity exec delete-docs.js --with-user-tokenImportant notes:
- Always test your GROQ query first in the Vision plugin to make sure you're targeting the right documents
- Consider exporting your data before bulk deletions as a backup
- Remember that deleting documents is permanent and cannot be undone
- You'll need a token with write permissions for programmatic deletions
- With large batches (>50 documents), you may experience timeouts when deleting one-by-one
The CLI approach is generally the quickest and safest for one-time bulk operations, while the JavaScript client approach is better for automated workflows or application logic.
Show original thread1 reply
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.