A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/mongodb-python-delete-data-drop-collection/ below:

MongoDB python | Delete Data and Drop Collection

MongoDB python | Delete Data and Drop Collection

Last Updated : 04 Jul, 2025

In MongoDB, deletion is key to efficient data management. PyMongo offers methods to delete a single document, multiple documents, all documents in a collection or the entire collection itself. Here's a quick overview of these techniques:

1. Delete a single document: Use delete_one() to remove only the first document that matches the filter.

res = my_collection.delete_one({"name": "Mr.Geek"})

To see the number of documents deleted :

print(res.deleted_count)

2. Delete Multiple Documents: Use delete_many() to remove all documents that match a specific condition.

res = my_collection.delete_many({"name": "Mr.Geek"})

To see the number of documents deleted :

print(res.deleted_count)

3. Delete All Documents in a Collection: Using delete_many({}): This deletes all documents in the collection but keeps the collection and its indexes.

res = my_collection.delete_many({})

To see the number of documents deleted :

print(res.deleted_count)

4. Drop the entire collection: If you want to remove all documents along with the collection and its indexes, use drop().

db.my_collection.drop()

Examples Example 1: Delete a single document Python
from pymongo import MongoClient
c = MongoClient("mongodb://localhost:27017/")
db = c["my_database"]
col = db["my_collection"]

col.insert_many([
    {"name": "Mr.Geek", "role": "Developer"},
    {"name": "Mr.Geek", "role": "Designer"}
])

res = col.delete_one({"name": "Mr.Geek"})
print(res.deleted_count)

Output

1

Explanation:

Example 2: Delete multiple documents Python
from pymongo import MongoClient
c = MongoClient("mongodb://localhost:27017/")
db = c["my_database"]
col = db["my_collection"]

col.insert_many([
    {"name": "Mr.Geek", "role": "Developer"},
    {"name": "Mr.Geek", "role": "Tester"},
    {"name": "Mr.Geek", "role": "Manager"}
])

res = col.delete_many({"name": "Mr.Geek"})
print(res.deleted_count)

Output

4

Explanation:

Example 3: Delete all documents in a collection Python
from pymongo import MongoClient
c = MongoClient("mongodb://localhost:27017/")
db = c["my_database"]
col = db["my_collection"]

col.insert_many([
    {"product": "Laptop"},
    {"product": "Phone"},
    {"product": "Tablet"}
])
res = col.delete_many({})
print(res.deleted_count)

Output

3

Explanation:

Example 4: Drop the entire collection Python
from pymongo import MongoClient
c = MongoClient("mongodb://localhost:27017/")
db = c["my_database"]
col = db["my_collection"]

col.insert_many([
    {"product": "Laptop"},
    {"product": "Phone"},
    {"product": "Tablet"}
])

db.my_collection.drop()
print("Collection dropped successfully.")

Output

Collection dropped successfully.

Explanation:

Related articles



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4