Find unused indexes and hide them before dropping (hideIndex)
MongoDBPerformanceCommand
// Usage counter per index (on this member, since the last restart):
db.musteri.aggregate([{ $indexStats: {} }, { $project: { name: 1, ops: "$accesses.ops", since: "$accesses.since" } }, { $sort: { ops: 1 } }])
// Hide it from the planner before dropping (the index keeps being maintained):
db.musteri.hideIndex("idx_email")
// Revert immediately if problems appear:
db.musteri.unhideIndex("idx_email")
// If the observation period passes without issues:
db.musteri.dropIndex("idx_email")What it does
$indexStats shows how many times each index has been used; hideIndex hides an index from the query planner without dropping it, so the effect of removing it can be tested reversibly.
When to use it
When cleaning up unnecessary indexes that add write overhead and storage.
Caution
Counters are per member and reset on restart; if reads go to secondaries, check all members and observe a long enough period (including month-end reports). Unique or TTL indexes with ops 0 may still be required: the unique constraint and TTL deletes do not show up in the counter. A hidden index is still maintained on writes, and a hidden unique index still enforces the constraint.