Last Updated : 23 Jul, 2025
Indexes are important in MongoDB for improving query performance, allowing the database to quickly find the documents that match query criteria. The dropIndex()
method in MongoDB enables developers to manage their collection's indexes by removing unnecessary or outdated indexes. However, it's important to note that we cannot drop the default index on the _id
field using this method.
In this article, we’ll explain the dropIndex()
method in detail, its syntax, usage, examples, and important considerations to help us manage indexes in MongoDB efficiently
MongoDB dropIndex() method allows for the removal of specified indexes from a collection, but it does not permit the deletion of the default index of the _id field. Additionally, hidden indexes can also be dropped using this method. Starting from MongoDB 4.4, if the specified index is still being built, the dropIndex() method will abort the building process of the index, providing developers with greater control over index management in MongoDB collections.
Key points about thedropIndex()
method:
_id
index, as MongoDB automatically creates and requires this index.dropIndex()
can abort the building process._id
indexes using the db.Collection_Name.dropIndex("*")
method. Instead, use the dropIndexes()
method to remove multiple indexes.Syntax:
db.Collection_Name.dropIndex(index : <document/string>)
Key Terms
dropIndex()
The dropIndex()
method returns a document with the following fields:
1
indicating the operation was successful.Let’s walk through a few practical examples using MongoDB's dropIndex()
method. In these examples, we are working with:
student
Collection: Create an Index on the name
Field
First of all we created an index on the name field using createIndex() method:
db.student.createIndex({name:2})
This creates an ascending index on the name
field. Now, let's check the created index using the getIndexes()
method.
db.student.getIndexes()
Output:
Explanation:
_id
index.name_1
index we just created on the name
field.To remove the index created on the name
field, we can use the index name (name_1
) with the dropIndex()
method.
Query:
db.student.dropIndex("name_1")
Output:
Explanation:
name_1
on the name
field is successfully dropped.nIndexesWas
shows that the number of indexes before dropping the index was 2, and ok: 1
indicates the operation was successful.We can also drop the index using the index specification document instead of the index name.
1. First, create an index on the name
field with descending order.
db.student.createIndex({ name: -1 })
2. Now, drop the index by specifying the index document.
db.student.dropIndex({ name: -1 })
Output:
Explanation:
name
field with descending order (name: -1
) is successfully dropped using the index specification document.nIndexesWas
shows the number of indexes before the operation (2), and ok: 1
confirms the success of the operation.dropIndex()
method removes one index at a time. To drop multiple indexes, use the dropIndexes()
method._id
Index: MongoDB does not allow dropping the default _id
index, which is essential for document identification.dropIndex()
can be used to abort the process.dropIndexes()
for All Non-_id
Indexes (MongoDB 4.2+): From MongoDB 4.2 onwards, you cannot use dropIndex("*")
to drop all non-_id
indexes. Instead, use dropIndexes()
to drop multiple indexes.The dropIndex()
method in MongoDB is a vital tool for index management, allowing developers to remove unnecessary or outdated indexes, optimize database performance, and free up storage. Understanding when and how to use dropIndex()
efficiently is key to managing a MongoDB database. From simple index deletions to handling hidden indexes and aborted index builds (starting in MongoDB 4.4), this method provides essential functionality for maintaining your MongoDB collections.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4