Mongodb compact

Hello team,

we have a 3 node sharded cluster and we are trying to delete the unused data from the collections, but after the delete the disk space was not released back to the OS.

can anyone provide me the details on running the compact command from a mongod instance? looks like running compact from mongos is not supported in 3.0.4

Its going to block the database.
You need to run it on each member.

The collection will reuse the space so if you’re adding data to it still, you may just want to leave it.

And obigatory: 3.0.4 is old, really old.

1 Like

Hi @personal_java and welcome in the MongoDB Community :muscle: !

MongoDB is currently in version 6.0.3.

V3.0.4 was born in March 2015 and reached end of life support in Feb 2018 according to MongoDB Legacy Support Policy.

So my first suggestion would be to update this cluster to 6.0.3 ASAP and there are good chances that your problem could just go away with the upgrade.

Are you still running on MMAPv1 or are you using WiredTiger?


reg 3.0.4 yea, we just have this one cluster and others are migrated to Atlas. we are in process of moving this one too… soon!

Do you have any specific commands that I need toexecute the compact command from mongod?


1 Like

Thank you so much for the reply
we are on wiredtiger

1 Like

I would go with db.runCommand ( { compact: 'mydb.mycollection', force: true } ). But yeah if you can move to Atlas, just forget about it and migrate. Problem solved.


The command is as @MaBeuLux88 replied.

But per the link in my previous reply MMAPv1 or wiredTiger will tield different results.

MMAPv1 wont’t release storage back. You’ll need to perform an initial sync in that case.

I tried running that from the mongos instance. but looks like it is not supported. How can we run it from a mongod instance ?

Direct connect on each mongod, and each secondary too, its not replicated.

Its on the page too: compact — MongoDB Manual

Thank you for the link. this is what I get when I try to connect to mongo shell from the instance where mongod is running.

MongoDB shell version: 3.0.4
connecting to: test
2022-12-01T15:55:08.719+0000 W NETWORK  Failed to connect to, reason: errno:111 Connection refused
2022-12-01T15:55:08.720+0000 E QUERY    Error: couldn't connect to server (, connection attempt failed
    at connect (src/mongo/shell/mongo.js:179:14)
    at (connect):1:6 at src/mongo/shell/mongo.js:179
exception: connect failed.

Here is our set uo : 
1. mongos server
 /opt/mongo/bin/mongos --configdb,,

2. 3 mongod servers 
 /opt/mongo/bin/mongod --dbpath /mongodb/configdb --port 27019 --storageEngine wiredTiger
 /opt/mongo/bin/mongod --dbpath /mongodb/sharddb --port 27018 --storageEngine wiredTiger

As per your commands(and standard for shard replset members) the port is 27018

1 Like

that is it. i’m able to connect. Thank you so much for your help.