
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeThere are several ways to delete multiple documents in Sanity, depending on your needs and where you're working:
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:
npm install -g @sanity/clinpm install -g groq-clixargs commandFor 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()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:
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.
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.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store