31

Everytime we delete larger amounts of data from our MongoDB using collection.remove(), this makes the database so slow that eventually our web servers go down. I believe this is because the remove operation locks the collection for longer periods of time.

We have a query that gives us all the documents we want to delete. However the query does not include a date/time field, so we can't use a TTL index.

Is there a way to remove data in a nice way, freeing the lock from time to time?

7
  • My hacky solution: Write a program that queries the IDs of all documents you want to delete, store them in an array / list. Then fire a remove command for each one of them. However this is very slow Oct 14, 2015 at 13:30
  • 1
    Is this an expiration thingy? Do the documents have a date field of some sort? Oct 15, 2015 at 7:50
  • Unfortunatly they don't, otherwise I would use a TTL index. It's more of a migration thingy. Oct 15, 2015 at 7:53
  • Ongoing? That sound more like a problem with your data model than with MongoDB. I need more info on that, like sample documents, deletion triggers and such. In general, no, there isn't a way of throttling removes. You could use bulk operations, but it sounds like this is a way of treating the symptoms, not the cause of the problem. Oct 15, 2015 at 14:39
  • 1
    This was years ago, I don't remember. Haven't used MongoDB much since then. Jul 1, 2021 at 6:14

1 Answer 1

38
+500

Using bulk operations

Bulk operations might be of help here. An unordered bulk.find(queryDoc).remove() basically is a version of db.collection.remove(queryDoc) optimized for large numbers of operations. It's usage is pretty straightforward:

var bulk = db.yourCollection.initializeUnorderedBulkOp()
bulk.find(yourQuery).remove()
bulk.execute()

Please see Bulk.find().remove() in the MongoDB docs for details.

The idea behind this approach is not to speed up the removal, but to produce less load. In my tests, the load was reduced by half and took slightly less time than a db.collection.remove(query).

Creating an index

However, a remove operations should not stale your instance to a point of freezing. I tested the removal of 12M documents on my 5 year old MacBook and while it put some load on it, it was far away from freezing, and took some 10 minutes. However, the field I used to query was indexed.

This leads me to the conclusion that probably you might be experiencing a collection scan. If I am right, here is what happens: Your query contains fields or a combination of fields not contained in an index or for which an index intersection can not be constructed. This forces the mongod in question to find, access and read those fields for each and every document in the database from disk.

So, it might be helpful to create an index containing each field in your query in background prior to the remove operation, however counterintuitive this is.

db.collection.createIndex(
  {firstFieldYouQueryBy:1,...,NthFieldYouQueryBy:1},
  {background:true}
)

Albeit this operation will be done in background, the shell will block. This might take a while. You can see the status by opening a second shell and use:

db.currentOp()

(You'll have to search a bit).

When the index is created (which you can check by using db.collection.getIndices()), your removal operations should be more efficient and hence faster. After the mass removal is done, you can of course delete the index, if not needed otherwise.

With an index, your prevent a collection scan, thereby speeding up the removal considerably.

Combining both approaches

It should be obvious that it makes sense to create the index first and issue the bulk command after the index is ready.

10
  • Hey Markus, thanks for your detailed answer! You are right, the query we have does not use an index. However, creating an index on our live replica set is also a problem, it also freezes our web application servers :-(. So we can't just create an index, we have to remove each node from the replica set one by one, create the index offline, and reattach it to the replica set. This seems a lot of effort, just to delete some documents. Also this involves a stepdown and an election of a new primary, which means the database is effectively down for some seconds. Oct 16, 2015 at 8:30
  • 1
    @BastianVoigt Contacted you via Skype, I think there is need to talk. Oct 16, 2015 at 9:35
  • 8
    Could someone explain how exactly the bulk operations manage to reduce the server load? From the documentation it's not quite clear to me... Oct 20, 2015 at 6:25
  • 1
    This is a great solution, but for practical purposes, people using this shall be aware of choosing between not writing data matching the filter in a query or accepting to delete such written documents during the time this query is executed. Here is a lesson learned.
    – Naman
    Jun 24, 2021 at 15:32
  • 1
    "In new code, use Bulk.find.delete() instead of Bulk.find.remove()." mongodb.com/docs/manual/reference/method/Bulk.find.remove/…
    – Kim Kern
    Oct 6, 2022 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.