Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does index size not decrease when deleting documents from a large mongodb collection?

Tags:

mongodb

Environment: MongoDB 4.2.7, Centos7

I have a collection with about 500 million document and an index that is about 8GB. If I delete half the documents, I would expect the indexSize to decrease by about 50%. But I don't. Why does it not go down? Is there a way to compact the index?

Just to verify that the index should be smaller, I copied 50% of the documents to a brand new mongodb instance and created the index there. The index is indeed about 50% less.

like image 413
AngularNerd Avatar asked Jul 25 '26 05:07

AngularNerd


1 Answers

Well, I just found the answer to my own question. MongoDB actually has a compact command:

https://docs.mongodb.com/manual/reference/command/compact/

This should rebuild the indexes let me reclaim the free space.

like image 56
AngularNerd Avatar answered Jul 27 '26 05:07

AngularNerd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!