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

dropIndexes

On this page

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 the db.collection.dropIndex() and db.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>}

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 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.
writeConcern document Optional. A document expressing the write concern of the drop command. Omit to use the default write concern.

Behavior

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.

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.

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.

Examples

  • To drop all non-_id indexes , specify "*" for the index (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 methods db.collection.dropIndex() and db.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" ] } )
    
←   dropConnections filemd5  →