How to delete multiple documents in Sanity?

1 replies
Last updated: Jun 9, 2020
How to delete multiple documents?
AI Update

There 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 delete

This 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 delete

Tools needed:

  • Sanity CLI: npm install -g @sanity/cli
  • GROQ CLI: npm install -g groq-cli
  • On Windows: Install FindUtils from GnuWin for the xargs command

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-token

Important 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 thread
1 reply
Answered in DM but leaving this here in case others are looking. You can delete multiple documents one by one by running the following command:
sanity documents delete <docId>

Alternatively, you can run a script to remove a batch of up to 1k documents at the same time, but be sure to make a dataset export/backup before 
🙂 https://github.com/sanity-io/sanity-recipes/blob/master/snippets/deleteDocsByFilter.js

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.

Was this answer helpful?