Navigation
This version of the documentation is archived and no longer supported.

db.collection.dropIndex()

On this page

Definition

db.collection.dropIndex(index)

Important

mongo Shell Method

This page documents a mongo method. This is not the documentation for database commands or language-specific drivers, such as Node.js. To use the database command, see the dropIndexes command.

For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.

Drops or removes the specified index from a collection.

Note

To get the index name or the index specification document for the db.collection.dropIndex() method, use the db.collection.getIndexes() method.

The db.collection.dropIndex() method takes the following parameter:

Parameter Type Description
index string or document

Optional. Specifies the index to drop. You can specify the index either by the index name or by the index specification document.

To drop a text index, specify the index name.

Starting in MongoDB 4.2, you cannot specify "*" to drop all non-_id indexes. Use db.collection.dropIndexes() instead.

To get the index name or the index specification document for the db.collection.dropIndex() method, use the db.collection.getIndexes() method.

Behavior

Starting in MongoDB 4.2, the dropIndex() 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.

db.collection.dropIndex() 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.dropIndex() releases the lock.

Prior to MongoDB 4.2, db.collection.dropIndex() obtained an exclusive lock on the parent database, blocking all operations on the database and all its collections until the operation completed.

Dropping an Index during Index Replication

Avoid dropping an index on a collection while any index is being replicated on a secondary. If you attempt to drop an index from a collection on a primary while the collection has a background index building on a secondary, reads will be halted across all namespaces and replication will halt until the background index build completes.

Example

Consider a pets collection. Calling the getIndexes() method on the pets collection returns the following indexes:

[
   {  "v" : 1,
      "key" : { "_id" : 1 },
      "ns" : "test.pets",
      "name" : "_id_"
   },
   {
      "v" : 1,
      "key" : { "cat" : -1 },
      "ns" : "test.pets",
      "name" : "catIdx"
   },
   {
      "v" : 1,
      "key" : { "cat" : 1, "dog" : -1 },
      "ns" : "test.pets",
      "name" : "cat_1_dog_-1"
   }
]

The single field index on the field cat has the user-specified name of catIdx [1] and the index specification document of { "cat" : -1 }.

To drop the index catIdx, you can use either the index name:

db.pets.dropIndex( "catIdx" )

Or you can use the index specification document { "cat" : -1 }:

db.pets.dropIndex( { "cat" : -1 } )
[1]During index creation, if the user does not specify an index name, the system generates the name by concatenating the index key field and value with an underscore, e.g. cat_1.