๐Ÿ‘€ Our most exciting product launch yet ๐Ÿš€ Join us May 8th for Sanity Connect
Last updated November 19, 2021

Bulk deletion using Sanity CLI

By Tomas Hensrud Gulla

Using 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!

Installing the tools needed

  • Install Sanity CLI. npm install -g @sanity/cli
  • Install GROQ CLI. npm install -g groq-cli
  • If you are running Windows, install FindUtils from GnuWin to get the xargs command. Then add its installation folder to the PATH environment variable.

Bulk delete

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

What happens

The one-liner consists of three parts.

  • The query, 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.
  • The conversion to ndjson, groq "*" -o ndjson, using GROQ CLI, piped to the next part.
  • Finally, xargs is used to pass the list of document ids in ndjson format as arguments to Sanity CLI's document delete, xargs sanity documents delete.

Warning

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.

Delete faster, without timeouts

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.

  • Faster!
  • Cheaper (you're saving that API calls for later)
  • No timeouts!

Use with caution! ๐Ÿ™‚

Sanity โ€“ build remarkable experiences at scale

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.

Other guides by author