I have came across in one of my readings that remove() function will delete the documents without any changes to the existing indexes.

for instance: "db.users.remove() - This will remove all of the documents in the users collection. This doesn’t actually remove the collection, and any indexes created on it will still exist."

I would like to understand why the indexes are retained when the actual referenced data is removed / deleted in that specific collection? is there any specific use case?

Thank you

  • Are you wondering about index keys not being removed, or the index definition not being removed? The difference is crucial. Feb 27 '17 at 11:27

The db.collection.remove() command is used to remove data from a collection. As such, documents will be removed from the collection and the indexes on the collection will be updated to no longer reference the removed documents.

Remove can be used in such a way to only remove a subset of data. From the docs:

db.products.remove( { qty: { $gt: 20 } } )

For example, you may want to remove a subset of data but retain the existing indexes to continue access the remaining data according to the existing access patterns.

This command contrasts from the db.collection.drop() command, which removes the entire collection along with its indexes.


In MongoDB v3.6+ (mine was v3.6.8), after a db.products.remove() operation, you can re-create the index by issuing a:


otherwise tools like MongoDB Compass may report an open collection to be empty if you removed several documents from the collection using another tool like the mongo CLI.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.