MongoDB has an administration command called dropIndexes
that drops one or more indexes (except the index on the _id
field) from the specified collection.
Example Indexes
Suppose we have a collection called posts
with the following indexes:
db.pets.getIndexes()
Result:
[ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_" }, { "v" : 2, "key" : { "title" : 1 }, "name" : "title_1" }, { "v" : 2, "key" : { "tags" : 1 }, "name" : "tags_1" }, { "v" : 2, "key" : { "_fts" : "text", "_ftsx" : 1 }, "name" : "body_text", "weights" : { "body" : 1 }, "default_language" : "english", "language_override" : "language", "textIndexVersion" : 3 } ]
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 collection name, as well as the index name or its specification document.
Example:
db.runCommand( { dropIndexes: "posts", index: "title_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 used the index specification document instead of its name, like this:
db.runCommand( { dropIndexes: "posts", index: { "title" : 1 } })
Drop Multiple Indexes
If you want to drop multiple indexes (but not all indexes), pass the index names in an array.
Example:
db.runCommand( {
dropIndexes: "posts",
index: [ "tags_1", "body_text" ]
})
Result:
{ "nIndexesWas" : 3, "ok" : 1 }
At this stage, let’s re-check our list of indexes:
db.posts.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).
When passing an array, text indexes can only be dropped by specifying the index name. In such cases, you can’t drop text indexes by specifying their specification document, like you can with other indexes.
Drop All Indexes
You can drop all indexes by using the asterisk wildcard (*
).
For this example, I’ve re-created the indexes. So the indexes look like this::
db.products.getIndexes()
Result:
[ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_" }, { "v" : 2, "key" : { "title" : 1 }, "name" : "title_1" }, { "v" : 2, "key" : { "tags" : 1 }, "name" : "tags_1" }, { "v" : 2, "key" : { "_fts" : "text", "_ftsx" : 1 }, "name" : "body_text", "weights" : { "body" : 1 }, "default_language" : "english", "language_override" : "language", "textIndexVersion" : 3 } ]
Now let’s drop all indexes at once:
db.runCommand( { dropIndexes: "posts", index: "*" })
Result:
{ "nIndexesWas" : 4, "msg" : "non-_id indexes dropped for collection", "ok" : 1 }
All indexes were dropped except for the _id
index (that 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 that remains.
MongoDB Documentation
See the MongoDB documentation for more information on the dropIndexes
command.