dropIndexes

  • dropIndexes
  • The dropIndexes command drops one or more indexes(except the index on the _id field) from the specifiedcollection.

The command has the following form:

  1. { dropIndexes: <string>, index: <string|document|arrayofstrings>, writeConcern: <document>}

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 atext index), or an array of theindex name. To drop a text index,specify the index names instead of the index specificationdocument.
  • To drop multiple indexes (Available starting in MongoDB4.2), specify an array of the index names.writeConcerndocumentOptional. A document expressing the write concern of the drop command.Omit to use the default write concern.

Behavior

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.

dropIndexes obtains an exclusive lock on the specified collectionfor the duration of the operation. All subsequent operations on thecollection must wait until dropIndexes releases thelock.

Prior to MongoDB 4.2, 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.

Examples

  • To drop all non-_id indexes , specify "" for the index(See [Indexes Named ]($8d766be795784050.md#index-asterisk)).
  1. db.runCommand( { dropIndexes: "collection", index: "*" } )
  • To drop a single index, issue the command by specifying the name ofthe index you want to drop. For example, to drop the index namedage_1, use the following command:
  1. db.runCommand( { dropIndexes: "collection", index: "age_1" })

The mongo shell provides the helper methodsdb.collection.dropIndex() anddb.collection.dropIndexes():

  1. db.collection.dropIndex("age_1");
  • To drop multiple indexes, issue the command by specifying an array ofthe index names:
  1. db.runCommand( { dropIndexes: "collection", index: [ "age_1", "age_1_status_1" ] } )

See also

db.collection.dropIndexes() and db.collection.dropIndex()