Deleting bulk documents in Sanity Studio using CLI or Vision
27 replies
Last updated: Aug 23, 2022
B
Hello Everyone
How to to delete bulk documents from sanity studio using CLI or vision ?
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:
Make sure to (1) update the filter to get the correct documents and (2) run the query first (e.g.,
) 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.
sanity documents query --api-version=v2022-08-22 "*[_type == 'post']._id" | npx groq-cli "*" -o ndjson | xargs sanity documents delete
sanity documents query --api-version=v2022-08-22 "*[_type == 'post']._id"
Aug 23, 2022, 6:53 PM
B
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
xargsin 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
B
this delete query is working fineis there any way to pass array of strings ?
Aug 23, 2022, 7:23 PM
B
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-clicommand 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
B
Okay
Aug 23, 2022, 7:31 PM
B
Thanks Alot for your help
Aug 23, 2022, 7:31 PM
B
Really Appreciated 🙂
Aug 23, 2022, 7:31 PM
B
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 send an array of IDs to
sanity documents query --api-version=v2021-03-25 "*[0..15]._id" > docs.txt
docs.txt?
Aug 23, 2022, 7:33 PM
B
yes
Aug 23, 2022, 7:37 PM
B
I can send
Aug 23, 2022, 7:38 PM
You can open that file and format it by:
1. removing the first line with
Then copy what’s left and paste it on your command line after
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
B
ok
Aug 23, 2022, 7:45 PM
B
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:
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.
vim -c '%s/.*\(\".\+\"\).*/\1/ | norm ggddGdd' docs.txt
Aug 23, 2022, 7:55 PM
B
I have successfully deleted the bulk content
Aug 23, 2022, 8:06 PM
B
I have no words to Thank you
Aug 23, 2022, 8:06 PM
B
god bless you
Aug 23, 2022, 8:06 PM
B
Actually I am new to sanity just build couple of projects
Aug 23, 2022, 8:07 PM
B
yes I really enjoyed
Aug 23, 2022, 8:08 PM
B
Thanks once again
Aug 23, 2022, 8:08 PM
B
hoping to learn more in future through your guidance
Aug 23, 2022, 8:10 PM
B
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.