
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeThe Sanity CLI doesn't have a direct "delete by query" command, but there are several effective ways to batch delete documents of a certain type:
The most straightforward CLI approach is to query for document IDs and pipe them to the delete command. From this bulk deletion guide, here's the recommended one-liner:
sanity documents query "*[_type == 'myDocument']._id" --apiVersion 2021-03-25
| groq "*" -o ndjson
| join
| xargs sanity documents deleteWhat's happening here:
join command combines the IDs into a single stringxargs passes them to the delete commandNote: You'll need to install groq-cli (npm install -g groq-cli) and the author's Console.Join tool for this to work smoothly.
For better control and error handling, use @sanity/client in a Node script. While client.delete({query}) isn't supported, you can query first and then delete in batches:
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 batchDelete() {
// Fetch all document IDs of the type you want to delete
const ids = await client.fetch('*[_type == "yourDocumentType"]._id')
// Delete in batches to avoid transaction limits
const batchSize = 100
for (let i = 0; i < ids.length; i += batchSize) {
const batch = ids.slice(i, i + batchSize)
const transaction = client.transaction()
batch.forEach(id => transaction.delete(id))
await transaction.commit()
console.log(`Deleted ${Math.min(i + batchSize, ids.length)} of ${ids.length}`)
}
}
batchDelete()This approach from the community discussions gives you progress tracking and better error handling.
sanity dataset export.The JavaScript client approach is generally more reliable for large-scale deletions since you get better error handling, progress tracking, and can implement retry logic if needed.
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