VOOZH about

URL: https://www.geeksforgeeks.org/python/how-to-drop-all-the-indexes-in-a-collection-using-pymongo/

⇱ How to Drop all the indexes in a Collection using PyMongo? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Drop all the indexes in a Collection using PyMongo?

Last Updated : 15 Jul, 2025

In PyMongo, the drop_indexes() method is used to remove all non-default indexes from a collection. It helps developers clean up or reset indexing strategies, especially during optimization or testing. The method retains the default _id index, which cannot be dropped.

Syntax

collection.drop_indexes()

Note: drop_indexes() method does not take any parameters, it simply removes all indexes on the collection.

Sample database used:

👁 python-mongodb-sample-database5

Viewing Existing Indexes

By default, every MongoDB collection has an index on the _id field to uniquely identify each document. Even if you delete all other indexes, the _id index will still remain and cannot be removed.

To list all current indexes on a collection, run:

👁 python-mongodb-drop-all-indexes-1

Where,

  • key: Field used for indexing.
  • name: Name of the index.
  • ns: Namespace (i.e., Database.Collection).

Example 1:

This code creates an index on the l_id field. It then prints the auto-generated name of the new index and displays all indexes present in the collection using index_information().

Output

👁 dropindex_output-1
Snapshot of Terminal showing Output of Adding Index to a Collection

Example 2:

In this Example all indexes are removed except default _id index using the drop_indexes() method, then prints the remaining indexes using index_information().

Output

👁 dropindex_output2
Snapshot of Terminal showing Output of Deleting Index of a Collection

Related Articles:

Comment
Article Tags: