Navigation
This is an upcoming (i.e. in progress) version of the manual.

db.collection.dropIndexes()

On this page

Definition

db.collection.dropIndexes()

mongo Shell Method

This page documents the mongo shell method, and does not refer to the MongoDB Node.js driver (or any other driver) method. For corresponding MongoDB driver API, refer to your specific MongoDB driver documentation instead.

Drops the specified index or indexes (except the index on the _id field) from a collection.

You can use the method to:

  • Drop all but the _id index from a collection.

    db.collection.dropIndexes()
    
  • 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):

      db.collection.dropIndexes( { a: 1, b: 1 } )
      
    • The index name:

      db.collection.dropIndexes( "a_1_b_1" )
      

      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:

    db.collection.dropIndexes( [ "a_1_b_1", "a_1", "a_1__id_-1" ] )
    

    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.

New in version 4.4: If an index specified to db.collection.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() returned an error if any of the specified indexes were still building. See Aborts In-Progress Index Builds for more complete documentation.

The db.collection.dropIndexes() is a wrapper around the dropIndexes command.

Behavior

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.

Aborts In-Progress Index Builds

New in version 4.4: If an index specified to db.collection.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.

The indexes specified to dropIndexes() must be the entire set of in-progress builds associated to a given index builder, i.e. the indexes built by 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 and drop the indexes.

Use currentOp to identify the index builds associated to a createIndexes / createIndexes() operation. See Active Indexing Operations for an example.

For replica sets or shard replica sets, aborting an index on the primary does not simultaneously abort secondary index builds. dropIndexes() attempts to abort the in-progress builds for the specified indexes on the primary and if successful creates an associated “abort” oplog entry. Secondary members with replicated in-progress builds wait for a commit or abort oplog entry from the primary before either committing or aborting the index build.