How can I bulk delete documents?

I have 15-20 millions of records in a single collection and I want to delete about 90% of documents.
I cant use deleteMany cuz I think my other code dependent on this db will crash. So is there any way to delete these records in chunks and faster?

To add more to this, I dont have index on the field with which I want to filter results.

Welcome to the community @Rebel.

As per your description, Looks like the useful data for you in that collection is only 10%. So easiest way I would suggest is: (Considering your data is on standalone and your collection isn’t sharded)

  1. Apply index on the fields with which you want to filter the results. (If it’s one or two fields with which you want to filter the results, You can apply indexes on it. If it’s confusing, Please post the sample record and the query that you’re intending to use)

  2. Write your filtered result to the new collection (I believe that is the 10% of the data that is really useful for you)

  3. Drop the older collection.

1 Like

My 2 cents.

The idea of writing the kept documents in a new collection is very very nice. However, I would skip building the index. As all documents have to be read in RAM to build the index, I would read them only once in the filtering code. With building the index, the chances that the 10% are read twice are high unless it is the last 10%.

The filtering idea of @viraj_thakrar has the added benifit that you reclaim the disk space of the old collection.