Introducing Dragonfly Cloud! Learn More

Question: How can I clean up disk space in MongoDB?

Answer

Cleaning up disk space in MongoDB is crucial for maintaining performance and preventing unnecessary costs, especially when dealing with large datasets. Here are some strategies and code examples to help manage disk space effectively:

1. Use db.collection.remove()

For collections where documents are frequently added and removed, use the remove() method to delete documents that are no longer needed.

// Remove all documents matching the condition db.collection.remove({ <condition> });

Note: This operation does not immediately free disk space but removes documents from collections, making space available for reuse by MongoDB.

2. Compact Collections with compact Command

The compact command rewrites and defragments data files for a collection within the same database. It requires additional disk space during its operation and locks the database, so consider running it during maintenance periods.

db.runCommand({ compact: '<collectionName>' });

3. Use TTL Indexes for Automatic Data Expiration

Time-To-Live (TTL) indexes automatically remove documents after a certain amount of time, freeing up disk space without manual intervention.

db.collection.createIndex({ "<fieldName>": 1 }, { expireAfterSeconds: <timeInSeconds> });

This is particularly useful for data that becomes irrelevant after a specific timeframe, such as logs or session information.

4. Drop Unused Collections and Databases

If certain collections or databases are no longer required, consider dropping them to reclaim disk space.

// Drop a collection db.collection.drop(); // Drop a database db.dropDatabase();

5. Run repairDatabase for Standalone Instances

The repairDatabase command can reclaim disk space for standalone MongoDB instances. It compacts collections, rebuilds indexes, and discards unused space. Note that it locks the database and may require an amount of free space equal to the size of your database.

db.adminCommand({ repairDatabase: 1 });

Conclusion

Managing disk space efficiently in MongoDB involves removing unnecessary data, compacting collections, utilizing TTL indexes for automatic cleanup, and, if necessary, dropping entire collections or databases. Always assess the impact of these operations on your application's availability and performance before proceeding.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Start building today 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.