This topic describes how to release disk space of an ApsaraDB for MongoDB instance.
WiredTiger is used as the default storage engine from MongoDB 3.2. The
storage.directoryPerDB:true parameter is defined on ApsaraDB for MongoDB. Each database is stored on disk as a separate folder and collections are separate files in the folder. For more information about the
storage.directoryPerDB:true parameter, see storage.directoryPerDB.
1. Check whether unused indexes, collections, or databases exist. Run the dropIndexes, drop, or dropDatabase commands to delete the unused indexes, collections, or databases. After the command is executed, the files are deleted immediately to release disk space.
2. Check whether unnecessary documents exist. Run the delete command to delete the unnecessary documents. Like MySQL, deleted MongoDB documents are just attached delete tags, indicating the corresponding storage space is marked as free and waits to be reused. Therefore, the delete command cannot release disk space immediately. If you want to release disk space immediately, perform the next step.
3. Check whether a large amount of free space exists in collections. Run the collStats command to compare the
freeStorageSize fields, and then run the compact command to reclaim the free physical space. For more information, see Defragment a disk to improve disk usage.
You must run the
compactcommand on each node.
In versions earlier than MongoDB 4.4, all other operations are blocked when you run
compactcommands. Proceed with caution.
Run the cloneCollection command or use the cursor.forEach method to copy all documents to a new collection, and then rename the collection to release disk space. This is only applicable to offline scenarios.