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

Bulk deleting a document type in Sanity.io and the need for a built-in feature.

4 replies
Last updated: Jun 16, 2022
Hi, what's the best way to bulk delete a particular document type. I'm in a scenario where there's a type that currently has about 20,000 test documents. I'd like to replace those documents with new versions completely and remove unused documents so the easiest way would be to clear it all out and do a bulk re-import. I've been trying different approaches motivated by links this forum, mainly https://www.sanity.io/schemas/delete-documents-by-filter-698e1f26 , but that script doesn't work well in trying to clear out 20,000 entries. The script works half the time and when it does, I'm only able to delete, say, 100 at a time but with 20,000 entries, that would take 200 calls. A loop could be built in but I'd hit the rate limit pretty quickly.
Example query: *[ _type == 'users' ][0...999]
Jun 16, 2022, 5:26 AM
I don’t think there is a better approach than this script. Run it on a loop for a while until you have no remaining document I’d say.
Jun 16, 2022, 7:40 AM
Ok thank you
user F
. Will keep trying then. Is there a plan to build something into sanity directly? Clearing a table should be a fast/easy few clicks. Saw a plugin was being developed that enables bulk deletes but it's still in alpha.
Jun 16, 2022, 11:55 AM
If you have 20,000 documents to delete, maybe it's better to just export the dataset without those documents, then work from that dataset?
Jun 16, 2022, 4:47 PM
user M
Ah, I see, yes, that could be an alternate to do the opposite approach. I'll give the alternate approach a try next time.
For the current example, after some persistence and looping with cooldown to prevent rate limiting, I was able to delete the 20,000 entries. And I have already reimported the updated data successfully.

Of note, this use case of clearing out a document type is probably pretty common so something built in to easily do so would be very helpful, even if that requires some sort of extra safe guard to prevent accidental wipes. Something I could've done with a few clicks in phpMyAdmin or MySQL Workbench took a few hours to try and piece together the code to do a similarly common function. Suggestion for the future.

Thanks much again Kitty and RD for your responses. Appreciate it!
Jun 16, 2022, 11:14 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?