Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The storage space isn't decreased after deleting doc from Db. Why?

Tags:

mongodb

I have deleted the documents of the collection by running deleteMany query on the shell and it returned acknowledged true and result.

Even after deleting around 1 million documents the size of the db is same while running show dbs command.

The delete query which I have executed on the shell:

db.getCollection('activity').deleteMany({createdAt:{$lte: 1565375400000}})

The result of the above query is:

{ "acknowledged" : true, "deletedCount" : 1199713 }

Before and after the query the output of show dbs command is:

admin           0.000GB
local           0.000GB
logDb          17.203GB

Why is it so that db size isn't decreased and what can I do to free that size?

like image 443
Digvijay Rathore Avatar asked Sep 05 '25 16:09

Digvijay Rathore


1 Answers

MongoDB will not release the space back to disk. Instead, it'll reserve the space considering the future growth

The right way to run an housekeeping is by dropping unwanted collections or drop unwanted DB's (make sure to take backup) which would release the space back to OS

As you've already deleted the docs, I would suggest you to run the compact command during maintenance window (if its a critical environment) which will defrag all the data and indexes and release the space back to OS

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

Thank you!

like image 158
Dexter Avatar answered Sep 09 '25 09:09

Dexter