Dragonfly Cloud is now available on the AWS Marketplace - Learn More

Question: How do you clear the cache in MongoDB?

Answer

In MongoDB, caching is primarily handled by the storage engine, with WiredTiger being the default storage engine in versions of MongoDB starting from 3.2. WiredTiger uses an internal cache to improve database performance by keeping frequently accessed data in memory. Sometimes, for various reasons such as performance testing or troubleshooting, you might need to clear this cache. However, MongoDB doesn't provide a direct command like clear cache as you might find in some other systems. Instead, you can achieve a similar effect through other operations.

Reducing Cache Size Temporarily

One method to effectively clear the cache is by setting the cache size to a smaller value and then restoring it to its original value. This operation forces MongoDB to evict data from the cache. Note that this approach might impact performance, so it should be done with caution, especially on production systems.

// Reduce the WiredTiger cache size to 100MB db.adminCommand({setParameter: 1, "wiredTigerEngineRuntimeConfig": "cache_size=100M"}); // Set it back to the original value, e.g., 1G db.adminCommand({setParameter: 1, "wiredTigerEngineRuntimeConfig": "cache_size=1G"});

Restarting the MongoDB Service

Another way to clear the cache is by restarting the MongoDB server. This action will completely clear the cache since all cached data is held in memory and will be lost on restart. On Linux systems, the service can be restarted using the following command:

sudo systemctl restart mongod

This method is very effective but has the downside of causing downtime, which makes it unsuitable for production environments without proper planning.

Dropping Databases or Collections

If the goal is to clear cached data related to specific databases or collections, dropping them will also drop the associated data from the cache. This is because the cache entries are invalidated and removed once the database or collection no longer exists.

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

Remember that these operations are destructive and cannot be undone. Always ensure you have backups or do not require the data before proceeding.

Conclusion

While MongoDB does not offer a straightforward clear cache command, you can manage the cache indirectly through configuration changes, service restarts, or structural modifications like dropping databases or collections. Each method comes with its own set of considerations regarding performance impact and potential data loss.

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

Switch & save up to 80% 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost