DBAtlas · Database operations atlas

Compact a collection

MongoDBMaintenance

Command

db.runCommand({ compact: "customer" })

What it does

Rewrites the collection and its indexes on disk, reclaiming free space.

When to use it

When you want to reclaim disk space after many deletes/updates.

Caution

In 4.4+, compact does not block reads/writes; it only blocks certain DDL operations on the same collection (drop, createIndex, dropIndex, etc.); it still generates heavy IO and does not always return space to the operating system. Run it on a secondary and in a maintenance window when possible.

Open in DBAtlas →

Related