Compact run on primary no space released

Did you ever find a solution? - I have the exact same problem. When I run db.getMongo().setSecondaryOk(); - I am able to run compact on the Slave and it releases some space. However when I run db.runCommand({ compact: collectionName }); on the Master, it immediately returns and no space is released.

Hi @Roman_Gaufman1,

Moved to a new topic as there isn’t enough information to guarantee that the issue is the same. I will link this back to the original if this isn’t is the case.

In saying the above, can you provide the following information?:

  1. MongoDB version you tried this on
  2. Confirmation if db.runCommand({ compact: collectionName }); was the command run on the node whilst it was still PRIMARY and if you used the force option - Note: as per the compact documentation for replica sets:

You should run compact on secondary nodes whenever possible.

  1. How much disk space are you expecting to be released? And to follow on, how are you calculating this? As per the Disk Space portion of the compact documentation:

On WiredTiger, compact attempts to reduce the required storage space for data and indexes in a collection, releasing unneeded disk space to the operating system. The effectiveness of this operation is workload dependent and no disk space may be recovered. This command is useful if you have removed a large amount of data from the collection, and do not plan to replace it.

  1. Context regarding use of compact - E.g. Was a large amount of data removed with no need or plans to replace it?

You may also find the FAQ: MongoDB Storage documentation useful regarding this scenario too. Specific to WiredTiger and deleted documents on the same page:

The WiredTiger storage engine maintains lists of empty records in data files as it deletes documents. This space can be reused by WiredTiger, but will not be returned to the operating system unless under very specific circumstances.

Regards,
Jason

Hello @Jason_Tran , I am facing same issue with my mongo db setup. I have 3 server mongo cluster. Here, 2 Servers are act as replication of Primary Server. After running db.collectionname.deletemany(), i am executing “db.runCommand({compact: ‘collectionname’,force: true});” . Both commands are executed successfully but compact is not able to release disk space. It is showing “0 bytes free”. My Mongo DB Version is 5.0.7
rs1:PRIMARY> db.runCommand({compact: ‘cdr’,force: true});
{
“bytesFreed” : 0,
“ok” : 1,
“$clusterTime” : {
“clusterTime” : Timestamp(1709533620, 1),
“signature” : {
“hash” : BinData(0,“AAAAAAAAAAAAAAAAAAAAAAAAAAA=”),
“keyId” : NumberLong(0)
}

    },

“operationTime” : Timestamp(1709533620, 1)
}
Thanks,
Ashish Pandya

Hello All, This is to inform you all that compact command is executed successfully on Primary and Free Disk Space is reclaimed successfully. Main reason is that If Mongo DB server doesn’t have enough disk space , Compact command will not able to free disk space. For example, Linux server which is used as Mongo have 8 GB of total disk space. Now , Some Data is seeded in Mongo DB hence Mongo DB is used 7 GB of data. OS/Server has available space of 1 GB only. Now, when we delete data of 500 MB and try to run compact, it won’t be able to free up bytes. You have to delete more data from mongo if you want to execute compact command or you can extend disk space size from 8 Gb to 16 GB in same mount point where Mongo DB is installed and occupied disk space.