Sanity

Easier bulk deletion of Sanity documents, using Sanity CLI

Yesterday I wrote a kind of intricate blog post on how to bulk delete Sanity documents using Sanity CLI, Notepad++ and regex. My closing phrase of the blog post was.

Do you know an easier way?

Luckily for me, Knut Melvær from Sanity knew and took the time out of his busy schedule to enlighten me in the comments section. With this newfound knowledge, I can write this very blog post on how to bulk delete with less hassle.

Installing the tools needed

Bulk delete

With the tools in place, I can bulk delete all documents of type myDocument with this one-liner.

sanity documents query "*[_type == 'myDocument'][0...20]._id" | groq "*" -o ndjson | xargs sanity documents delete

What happens

The one-liner consists of three parts.

Warning

I have been experiencing timeout when deleting more than 50 documents in one go, that's why I have limited to query above to 20. This should not be a problem, so Sanity is digging into it...

Closing words

My motivation for writing blog posts is not to spread the knowledge I possess. My motivation is the knowledge I gain from researching the blog post, and the knowledge I gain from the feedback I receive after publishing the blog post.