dropIndexes
dropIndexes
The
dropIndexes
command drops one or more indexes (except the index on the_id
field) from the specified collection.Tip
In the
mongo
Shell, this command can also be run through thedb.collection.dropIndex()
anddb.collection.dropIndexes()
helper methods..Helper methods are convenient for
mongo
users, but they may not return the same level of information as database commands. In cases where the convenience is not needed or the additional return fields are required, use the database command.The command has the following form:
{ dropIndexes: <string>, index: <string|document|arrayofstrings>, writeConcern: <document>, comment: <any> } The command takes the following fields:
FieldTypeDescriptiondropIndexesStringThe name of the collection whose indexes to drop.indexstring or document or array of stringsThe index or indexes to drop.
To drop all but the _id index from the collection, 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.
To drop multiple indexes (Available starting in MongoDB 4.2), specify an array of the index names.
writeConcerndocumentOptional. A document expressing the write concern of thedrop
command. Omit to use the default write concern.comment
anyOptional. 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.
Behavior
Kill related queries only
Starting in MongoDB 4.2, the dropIndexes
operation only kills
queries that are using the index being dropped. This may include
queries considering the index as part of
query planning.
Prior to MongoDB 4.2, dropping an index on a collection would kill all open queries on the collection.
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.
Abort In-Progress Index Builds
New in version 4.4: If an index specified to dropIndexes
is still building,
dropIndexes
attempts to abort the in-progress build.
Aborting an index build has the same effect as dropping the built
index. Prior to MongoDB 4.4, dropIndexes
would return an
error if the collection had any in-progress index builds.
For replica sets, run dropIndexes
on the primary.
The primary aborts the index build and creates an associated
"abortIndexBuild" oplog entry. Secondaries which replicate the
"abortIndexBuild" oplog entry abort the in-progress index build and
discard the build job. See Index Build Process for detailed
documentation on the index build process and the specific timing for
terminating an in-progress index build.
The indexes specified to dropIndexes
must be the entire set
of in-progress builds associated to a single createIndexes
or db.collection.createIndexes()
operation. To drop a specific
index out of a set of related in-progress builds, wait until the index
builds complete and specify that index to dropIndexes
.
For example, a createIndexes
/
createIndexes()
operation creates three
indexes. Assuming all three index builds are in-progress,
dropIndexes
must specify all three indexes to abort the
index builds.
Use currentOp
to identify the index builds associated to a
createIndexes
/ createIndexes()
operation. See Active Indexing Operations for an example.
Hidden Indexes
Starting in version 4.4, MongoDB adds the ability to hide or unhide indexes from the query planner. By hiding an index from the planner, users can evaluate the potential impact of dropping an index without actually dropping the index.
If after the evaluation, the user decides to drop the index, the user can drop the hidden index; i.e. you do not need to unhide it first to drop it.
If, however, the impact is negative, the user can unhide the index instead of having to recreate a dropped index. And because indexes are fully maintained while hidden, the indexes are immediately available for use once unhidden.
For more information on hidden indexes, see Hidden Indexes.
Examples
To drop all non-
_id
indexes , specify"*"
for theindex
(See Indexes Named*
).db.runCommand( { dropIndexes: "collection", index: "*" } ) 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:db.runCommand( { dropIndexes: "collection", index: "age_1" }) The
mongo
shell provides the helper methodsdb.collection.dropIndex()
anddb.collection.dropIndexes()
:db.collection.dropIndex("age_1"); To drop multiple indexes, issue the command by specifying an array of the index names:
db.runCommand( { dropIndexes: "collection", index: [ "age_1", "age_1_status_1" ] } )