How to Drop all the indexes in a Collection using PyMongo? Read Courses Practice Improve Improve Improve Like Article Like Save Article Save Report issue Report Prerequisites: MongoDB and Python With the help of drop_indexes() method we can drop all the indexes in a Collection. No parameter is passed in the method. Only default index _id can not be deleted. All the Non _id indexes will be the drop by this method. It means we can only drop the index which we have created. Syntax: db.collection_name.drop_indexes() Sample database used: By default, each collection has the _id index. All collections compulsorily have at least one index. If all indexes are removed, then a new index will be automatically generated. We can see the indexes present by running the following command: Now, we can run the following code to add a new Index called newIndex to the collection, given that the mongo server is running: Example 1: Adding an Index to the Collection import pprint import pymongo # connection try: client = pymongo.MongoClient() db = client['GFG'] print('connection to the server established') except Exception: print('Failed to Connect to server') collection = db.lecture # creating an index resp = collection.create_index("l_id") # printing the auto generated name # returned by MongoDB print(resp) # index_information() is analogous # to getIndexes pprint.pprint(collection.index_information()) Output: Example 2: Deleting the Index from the Collection import pprint import pymongo try: client = pymongo.MongoClient() db = client['GFG'] print('connection to the server established') except Exception: print('Failed to Connect to server') collection = db.lecture # dropping the index using autogenerated # name from MongoDB collection.drop_indexes() # printing the indexes present on the collection pprint.pprint(collection.index_information()) Output: Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills, become a part of the hottest trend in the 21st century.Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve. Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore. Last Updated : 10 Jun, 2020 Like Article Save Article Previous Convert PyMongo Cursor to JSON Next Geospatial Queries with Python MongoDB Share your thoughts in the comments Add Your Comment Please Login to comment...