19. Creating, Listing, Hiding, and Dropping Indexes
Use createIndex() to create an index, getIndexes() to inspect indexes, hideIndex() and unhideIndex() to test planner behavior, and dropIndex() to remove an index.
Before dropping an index, review query telemetry, application code, scheduled jobs, and rare but important query patterns. An index that appears unused during a short observation window may still support a monthly report or operational task.
Index builds can consume resources. Follow deployment procedures appropriate for your MongoDB version and topology, and avoid casually building large indexes during peak traffic without assessing impact.
Do not remove an index merely because another index has a similar name or shares some fields. Compare field order, options, uniqueness, partial filters, and actual workload needs.
Validate the impact before dropping an index. Hiding it first can provide a reversible test, but the index still consumes storage and write resources while hidden.
db.orders.getIndexes();
db.orders.hideIndex("status_1");
// Observe relevant workloads and plans.
db.orders.unhideIndex("status_1");
// Drop only after confirming it is safe.
// db.orders.dropIndex("status_1");- Use getIndexes() to inspect current index definitions.
- Hidden indexes can help evaluate the effect of planner exclusion.
- Review actual workloads before dropping indexes.
- Plan index builds and removals as operational changes.
What should you do before dropping a production index?