db.collection.dropIndexes()
Definition
mongo
Shell Method
This page documents the mongo
shell method, and doesnot refer to the MongoDB Node.js driver (or any other driver)method. For corresponding MongoDB driver API, refer to your specificMongoDB driver documentation instead.
Drops the specified index or indexes (except the index on the_id
field) from a collection.
You can use the method to:
- Drop all but the
_id
index from a collection.
- db.collection.dropIndexes()
Drop a specified index from a collection. To specify the index,you can pass the method either:
- The index specification document (unless the index is atext index in which case, use theindex name to drop):
- db.collection.dropIndexes( { a: 1, b: 1 } )
-
The index name:
- db.collection.dropIndexes( "a_1_b_1" )
Tip
To get the names of the indexes, use thedb.collection.getIndexes()
method.
- Drop specified indexes from a collection. (Available starting inMongoDB 4.2). To specify multiple indexes to drop, pass the methodan array of index names:
- db.collection.dropIndexes( [ "a_1_b_1", "a_1", "a_1__id_-1" ] )
If the array of indexnames includes a non-existent index, the method errors withoutdropping any of the specified indexes.
Tip
To get the names of the indexes, use thedb.collection.getIndexes()
method.
The db.collection.dropIndexes()
method takes the followingoptional parameter:
ParameterTypeDescriptionindexes
string or document or array of stringsOptional. Specifies the index or indexes to drop.
To drop all but the _id index from the collection,omit the parameter.
To drop a single index, specify either the index name, theindex specification document (unless the index is atext index), or an array of theindex name. To drop a text index,specify the index name or an array of the index name insteadof the index specification document.
To drop multiple indexes (Available starting in MongoDB4.2), specify an array of the index names.
The db.collection.dropIndexes()
is a wrapper around thedropIndexes
command.
Behavior
Kill related queries only
Starting in MongoDB 4.2, the dropIndexes()
operation only killsqueries that are using the index being dropped. This may includequeries considering the index as part ofquery planning.
Prior to MongoDB 4.2, dropping an index on acollection would kill all open queries on the collection.
Resource Locking
Changed in version 4.2.
db.collection.dropIndexes()
obtains an exclusive lock on the specified collectionfor the duration of the operation. All subsequent operations on thecollection must wait until db.collection.dropIndexes()
releases thelock.
Prior to MongoDB 4.2, db.collection.dropIndexes()
obtained an exclusivelock on the parent database, blocking all operations on thedatabase and all its collections until the operation completed.
Index Names
If the method is passed an array of index names that includes anon-existent index, the method errors without dropping any of thespecified indexes.
_id Index
You cannot drop the default index on the _id
field.
text Indexes
To drop a text index, specify the index nameinstead of the index specification document.