👀 Our most exciting product launch yet 🚀 Join us May 8th for Sanity Connect

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 ?
Aug 23, 2022, 6:41 PM
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.
Aug 23, 2022, 6:53 PM
I am using windows , following the documents I have to install GNU Win?
Aug 23, 2022, 7:06 PM
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
.
Aug 23, 2022, 7:12 PM
this delete query is working fineis there any way to pass array of strings ?
Aug 23, 2022, 7:23 PM
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?
Aug 23, 2022, 7:24 PM
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.
Aug 23, 2022, 7:27 PM
Okay
Aug 23, 2022, 7:31 PM
Thanks Alot for your help
Aug 23, 2022, 7:31 PM
Really Appreciated 🙂
Aug 23, 2022, 7:31 PM
You’re welcome!
Aug 23, 2022, 7:31 PM
I will try to make that work 😕
Aug 23, 2022, 7:32 PM
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
?
Aug 23, 2022, 7:33 PM
yes
Aug 23, 2022, 7:37 PM
I can send
Aug 23, 2022, 7:38 PM
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
.
Aug 23, 2022, 7:39 PM
ok
Aug 23, 2022, 7:45 PM
I am doing
Aug 23, 2022, 7:45 PM
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.
Aug 23, 2022, 7:55 PM
I have successfully deleted the bulk content
Aug 23, 2022, 8:06 PM
I have no words to Thank you
Aug 23, 2022, 8:06 PM
god bless you
Aug 23, 2022, 8:06 PM
Actually I am new to sanity just build couple of projects
Aug 23, 2022, 8:07 PM
Great! Glad you’re here and hope you’ve enjoyed it so far. 😀
Aug 23, 2022, 8:08 PM
yes I really enjoyed
Aug 23, 2022, 8:08 PM
Thanks once again
Aug 23, 2022, 8:08 PM
hoping to learn more in future through your guidance
Aug 23, 2022, 8:10 PM
stay blessed!
Aug 23, 2022, 8:10 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?