Deleting bulk documents in Sanity Studio using CLI or Vision

27 replies
Last updated: Aug 23, 2022
Hello Everyone
How to to delete bulk documents from sanity studio using CLI or vision ?
AI Update

There are two officially documented ways to bulk delete documents from Sanity:

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 doc3

Important 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.

Hi there. You can’t delete using Vision (it’s read-only), but you can through the CLI with this:

sanity documents query --api-version=v2022-08-22 "*[_type == 'post']._id" | npx groq-cli "*" -o ndjson | xargs sanity documents delete
Make sure to (1) update the filter to get the correct documents and (2) run the query first (e.g.,
sanity documents query --api-version=v2022-08-22 "*[_type == 'post']._id"
) to ensure you’re working with the correct documents. This deletes data so it may be best to test on a non-production dataset first.
I am using windows , following the documents I have to install GNU Win?
Ah. I’m not too sure on how you’d implement
xargs
in Windows. As an alternative, you can query for all the document ids you want to delete, format them into whitespace-separated
"strings" "like" "this"
, and then pass that into
sanity documents delete
.
this delete query is working fineis there any way to pass array of strings ?
like sanity documents deleteand then instead of passing strings as i want to delete bulk , so is there any way to pass array directly containing the ids?
I don’t believe it takes an array of strings, but it will take multiple whitespace-separated strings at the same time. That’s the function of the
groq-cli
command in the original command snippet. Perhaps you can get that working on Windows, or you can format the list of documents yourself.
Okay
Thanks Alot for your help
Really Appreciated 🙂
You’re welcome!
I will try to make that work 😕
Can you pipe the output of a command to a file in your Windows terminal? For example, does
sanity documents query --api-version=v2021-03-25 "*[0..15]._id" > docs.txt
send an array of IDs to
docs.txt
?
yes
I can send
You can open that file and format it by:
1. removing the first line with
[
,2. removing the last line with
]
, and3. deleting the commas at the end of each line.
Then copy what’s left and paste it on your command line after
sanity documents delete
.
ok
I am doing
I don’t want to complicate things, but if you have vim on your machine you could run this in your terminal on your file that’s output:

vim -c '%s/.*\(\".\+\"\).*/\1/ | norm ggddGdd' docs.txt
It looks complicated but all it’s doing is running a few commands upon opening docs.txt. If you don’t have vim installed then it won’t work, though you could use other terminal commands to do the same thing. In this case, a quick edit in VS Code or similar should work fine.
I have successfully deleted the bulk content
I have no words to Thank you
god bless you
Actually I am new to sanity just build couple of projects
Great! Glad you’re here and hope you’ve enjoyed it so far. 😀
yes I really enjoyed
Thanks once again
hoping to learn more in future through your guidance
stay blessed!

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?