MongoDB dropIndexes()

In MongoDB, the dropIndexes() method allows you to drop one or more indexes from a collection.

To drop a single index, pass the name of the index or its definition/specification document. If it’s a text index, you can only specify the index name.

To drop multiple indexes, pass the index names in an array.

To drop all indexes (except for the _id index), don’t pass any arguments.

Example Indexes

Suppose we have a collection called pets. We can use getIndexes() to see what indexes it has:

db.pets.getIndexes()

Result:

[
	{
		"v" : 2,
		"key" : {
			"_id" : 1
		},
		"name" : "_id_"
	},
	{
		"v" : 2,
		"key" : {
			"type" : 1
		},
		"name" : "type_1"
	},
	{
		"v" : 2,
		"key" : {
			"name" : 1
		},
		"name" : "name_1",
		"hidden" : true
	},
	{
		"v" : 2,
		"key" : {
			"weight" : -1
		},
		"name" : "weight_-1"
	}
]

In this case we’ve got four indexes. The first one is the default index for the _id field. This index is created automatically when you create a collection, and it can’t be dropped.

The other three indexes can be dropped.

Drop a Single Index

To drop a single index, pass the index name or its specification document.

Example:

db.pets.dropIndexes("weight_-1")

In this case, we’re passing the index name.

Running that results in the following output:

{ "nIndexesWas" : 4, "ok" : 1 }

This tells us that the index was dropped.

Alternatively, we could have dropped the index by passing its specification document:

db.pets.dropIndexes( { "weight" : -1 } )

Drop Multiple Indexes

If you want to drop multiple indexes, but not all indexes, pass the index names in an array.

Example:

db.pets.dropIndexes( [ "type_1", "name_1" ])

Result:

{ "nIndexesWas" : 3, "ok" : 1 }

At this stage, let’s re-check our list of indexes:

db.pets.getIndexes()

Result:

[ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_" } ]

As expected, three indexes have been deleted and there’s only one left (and it can’t be deleted).

Drop All Indexes

You can drop all indexes by calling the dropIndexes() method without any arguments.

For this example, let’s drop the indexes on a collection called products. This collection has four indexes as follows:

db.products.getIndexes()

Result:

[
	{
		"v" : 2,
		"key" : {
			"_id" : 1
		},
		"name" : "_id_"
	},
	{
		"v" : 2,
		"key" : {
			"product.$**" : 1
		},
		"name" : "product.$**_1"
	},
	{
		"v" : 2,
		"key" : {
			"sizes" : 1
		},
		"name" : "sizes_1"
	},
	{
		"v" : 2,
		"key" : {
			"stock" : -1
		},
		"name" : "stock_-1"
	}
]

Now let’s drop all indexes:

db.products.dropIndexes()

Result:

{
	"nIndexesWas" : 4,
	"msg" : "non-_id indexes dropped for collection",
	"ok" : 1
}

As the message alluded to, all indexes were dropped except for the _id index. This index can’t be dropped.

Let’s check the indexes again.

db.products.getIndexes()

Result:

[ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_" } ]

As expected, the _id index is the only one left.

The dropIndexes() method is a wrapper around the dropIndexes command.

MongoDB Documentation

See the MongoDB documentation for more information on the dropIndexes() method.