Reclaim disk space after delete many in mongodb replicaset

Hello,
I have mongodb replicaset with one primary and two secondaries (running as statefulset in K8s). Few days ago when running tests, the disks went full. After allocating more diskspace we managed to get mongodb back to running. Then we deleted with ‘deletemany’ documents from the db. This did not reclaim the disk, as the compact is needed to run.

But, when running the compact, I cannot use loop in secondaries, like:

db.getCollectionNames().forEach(function(collname) {print('Compacting: ’ + collname);});

So I cannot loop trought all the collections. Also I need to run the compact commands per collection, on all nodes. First on secondaries then change primary to secondary and there as well.
Is this the correct process, is what I am thinking.

Thanks in any advance!

This is the recommended way to compact on a replica set.

If the remaining data is small enough it may be faster to perform an initial sync.

Delete the data and journal (if you have it separate) pvc of one secondary at a time. Step down primary and repeat.

Thank you! But,

Then, what would be right way to run the compact after delete? You cannot run compact on primary?

And, why would I delete data per node? I tought delete many is the right way to delete records, and the compact is needed to reclaim the space.

Yes deletemany is the correct way to remove documents from your collection.

If you are going to run the compact command then you do it as you’ve outlined. First on the secondaries and then step down the primary and run on that node as it’s a secondary now.

The other method @chris mentioned is doing an initial sync on each node after doing the delete many. Because if you delete the data and journal files when the data is replicated it’ll only claim the space used by the documents.

2 Likes