- Reference >
- Database Commands >
- Administration Commands >
- dropIndexes
dropIndexes¶
-
dropIndexes
¶ Changed in version 6.0.
The
dropIndexes
command drops one or more indexes (except the index on the_id
field and the last remaining shard key index, if one exists) from the specified collection.The command has the following form:
The command takes the following fields:
Field Type Description dropIndexes String The name of the collection whose indexes to drop. index string or document or array of strings The index or indexes to drop.
- To drop all indexes except the
_id
index and the last remaining shard key index from the collection if one exists, specify"*"
. - 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 names instead of the index specification
document. If this index is the last remaining shard key
index,
dropIndexes
raises an error. - To drop multiple indexes (Available starting in MongoDB 4.2), specify an array of the index names.
writeConcern document Optional. A document expressing the write concern of the drop
command. Omit to use the default write concern.comment
any Optional. A user-provided comment to attach to this command. Once set, this comment appears alongside records of this command in the following locations:
- mongod log messages, in the
attr.command.cursor.comment
field. - Database profiler output, in the
command.comment
field. currentOp
output, in thecommand.comment
field.
A comment can be any valid BSON type (string, integer, object, array, etc).
New in version 4.4.
- To drop all indexes except the
Behavior¶
Starting in MongoDB 6.0, dropIndexes
raises an error if you attempt
to use it to remove the last remaining shard key compatible index.
Passing "*"
to 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 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.
dropIndexes
obtains an exclusive lock on the specified collection
for the duration of the operation. All subsequent operations on the
collection must wait until dropIndexes
releases the
lock.
Prior to MongoDB 4.2, 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 dropIndexes
is still
building, 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, dropIndexes
returns an error if
there are any index builds in progress on the collection.
For replica sets, run 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.
Examples¶
To drop all non-
_id
indexes , specify"*"
for theindex
(See Indexes Named *).To drop a single index, issue the command by specifying the name of the index you want to drop. For example, to drop the index named
age_1
, use the following command:mongosh
provides the helper methodsdb.collection.dropIndex()
anddb.collection.dropIndexes()
:To drop multiple indexes, issue the command by specifying an array of the index names: