- Reference >
mongosh
Methods >- Collection Methods >
- db.collection.dropIndexes()
db.collection.dropIndexes()¶
On this page
Definition¶
-
db.collection.
dropIndexes
()¶ Important
mongosh
MethodThis page documents a
mongosh
method. This is not the documentation for a language-specific driver such as Node.js.For MongoDB API drivers, refer to the language-specific :driver:`MongoDB driver documentation </>`.
For the legacy
mongo
shell documentation, refer to the documentation for the corresponding MongoDB Server release:Drops the specified index or indexes (except the index on the
_id
field and the last remaining shard key index) from a collection.You can use the method to:
Drop all but the
_id
index from a collection.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 a text index in which case, use the index name to drop):
The index name:
Tip
To get the names of the indexes, use the
db.collection.getIndexes()
method.
Drop specified indexes from a collection. (Available starting in MongoDB 4.2). To specify multiple indexes to drop, pass the method an array of index names:
If the array of index names includes a non-existent index, the method errors without dropping any of the specified indexes.
Tip
To get the names of the indexes, use the
db.collection.getIndexes()
method.
The
db.collection.dropIndexes()
method takes the following optional parameter:Parameter Type Description indexes
string or document or array of strings Optional. 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, the index specification document (unless the index is a text index), or an array of the index name. To drop a text index, specify the index name or an array of the index name instead of the index specification document.
To drop multiple indexes (Available starting in MongoDB 4.2), specify an array of the index names.
The
db.collection.dropIndexes()
is a wrapper around thedropIndexes
command.
Behavior¶
Starting in MongoDB 6.0, db.collection.dropIndexes()
raises an error if you attempt
to use it to remove the last remaining shard key compatible index.
Passing "*"
to db.collection.dropIndexes()
drops all indexes except
the _id
index and the last remaining shard key compatible index,
if one exists.
Starting in MongoDB 5.2, you can use db.collection.dropIndexes()
to drop existing
indexes on the same collection even if there is a build in progress on
another index. In earlier versions, attempting to drop a different
index during an in-progress index build results in a
BackgroundOperationInProgressForNamespace
error.
Resource Locking¶
Changed in version 4.2.
db.collection.dropIndexes()
obtains an exclusive lock on the specified collection
for the duration of the operation. All subsequent operations on the
collection must wait until db.collection.dropIndexes()
releases the
lock.
Prior to MongoDB 4.2, db.collection.dropIndexes()
obtained an exclusive
lock on the parent database, blocking all operations on the
database and all its collections until the operation completed.
Index Names¶
If the method is passed an array of index names that includes a non-existent index, the method errors without dropping any of the specified indexes.
_id
Index¶
You cannot drop the default index on the _id
field.
text Indexes¶
To drop a text index, specify the index name instead of the index specification document.
Stop In-Progress Index Builds¶
Starting in MongoDB 4.4, if an index specified to db.collection.dropIndexes()
is still
building, db.collection.dropIndexes()
attempts to stop the in-progress build. Stopping
an index build has the same effect as dropping the built index. In
versions earlier than MongoDB 4.4, db.collection.dropIndexes()
returns an error if
there are any index builds in progress on the collection.
For replica sets, run db.collection.dropIndexes()
on the primary.
The primary stops the index build and creates an associated
“abortIndexBuild” oplog entry. Secondaries which replicate
the “abortIndexBuild” oplog entry stop the in-progress index build and
discard the build job. See Index Build Process for detailed
documentation on the index build process.
Use currentOp
to identify the index builds associated with
a createIndexes
or db.collection.createIndexes()
operation. See Active Indexing Operations for an example.