GROQ-powered webhooks - Send messages to Slack
Learn how to use the new GROQ-powered webhooks to send messages directly to Slack!
Go to GROQ-powered webhooks - Send messages to SlackUsing Sanity Studio, you can delete documents one by one. What if you want to delete hundreds of documents, or you have removed the schema definition so that your documents are no longer visible in Sanity Studio?
You can use the Vision plugin to query your data using GROQ, but you can not use Vision to do mutations, i.e. updating the content.
You can, of course, write code that does the mutations, or you can create a migration script and execute it from Sanity CLI - or you can use the Sanity CLI (and friends) only!
npm install -g @sanity/cli
npm install -g groq-cli
xargs
command. Then add its installation folder to the PATH environment variable.With the tools in place, I can bulk delete all documents of type myDocument
with this one-liner. The command is split over multiple lines for readability.
sanity documents query "*[_type == 'myDocument'][0...20]._id" --apiVersion 2021-03-25 | groq "*" -o ndjson | xargs sanity documents delete
The one-liner consists of three parts.
sanity documents query "*[_type == 'myDocument'][0...20]._id"
, using Sanity CLI that yields a list of 20 documents ids, in json format, piped to the next part.groq "*" -o ndjson
, using GROQ CLI, piped to the next part.xargs sanity documents delete
.The command above will perform one API call for every delete, and with >50 documents I have been experiencing timeouts. That's why I've limited the query to 20 documents.
Another option could be using jq join on Linux to join the document ids to a single string, passing it to sanity documents delete, and thus performing a single API call. I was not able to make this work on Windows so I made a small command line tool that fills the empty spot in the chain. Read on for details.
In addition to Sanity CLI, GROQ CLI, and xargs you will need my Console.Join. Download and include the folder in the PATH environment variable. Then add the join
command after GROQ CLI has converted the json to ndjson, and right before passing the result to sanity documents delete.
sanity documents query "*[_type == 'myDocument']._id" --apiVersion 2021-03-25 | groq "*" -o ndjson | join | xargs sanity documents delete
With 250 documents to delete, the API will now be called once. Without the join
command the API would be called 250 times. Enjoy the following benefits.
Use with caution! 🙂
Sanity Composable Content Cloud is the headless CMS that gives you (and your team) a content backend to drive websites and applications with modern tooling. It offers a real-time editing environment for content creators that’s easy to configure but designed to be customized with JavaScript and React when needed. With the hosted document store, you query content freely and easily integrate with any framework or data source to distribute and enrich content.
Sanity scales from weekend projects to enterprise needs and is used by companies like Puma, AT&T, Burger King, Tata, and Figma.
Learn how to use the new GROQ-powered webhooks to send messages directly to Slack!
Go to GROQ-powered webhooks - Send messages to Slack