In this tutorial, we will see how to delete MongoDB documents using the python pymongo module.

Delete MongoDB Document:

If you follow along with our earlier MongoDB CRUD examples, you can notice that the following documents which we had in the previous tutorial.

{'_id': ObjectId('6197f62a5312dd9e82102c6b'), 'f_name': 'Rob', 'l_name': 'Carpenter'}
{'_id': ObjectId('6197f7eb588c1566078fbff2'), 'f_name': 'Rob', 'l_name': 'Carpenter'}
{'_id': ObjectId('619800106f7cca5b81de7ec7'), 'f_name': 'Nilesh', 'l_name': 'Mil'}
{'_id': ObjectId('619800106f7cca5b81de7ec8'), 'f_name': 'Andy', 'l_name': 'Apple'}
{'_id': ObjectId('619800106f7cca5b81de7ec9'), 'f_name': 'AMad', 'l_name': 'Amhed'}
{'_id': 1, 'f_name': 'Nilesh', 'l_name': 'Mil'}
{'_id': 2, 'f_name': 'Andy', 'l_name': 'Apple'}
{'_id': 3, 'f_name': 'AMad', 'l_name': 'Amhed'}

Let’s delete some of the documents from above now.

Deleting the documents  MongoDB can be happen using two different ways – Deleting a single document using some specific condition and deleting all records at a time, these are much as similar to any relational database.

delete_one() function:

The delete_one() function is used to remove a single document. We can also say it’s a conditional operation so that we have to provide a specific condition to this function as a parameter.

If at all the condition is met with multiple results, it deletes the very first document of the total result.

If we don’t provide an argument to this method then it will return an error message as:

delete_one() missing 1 required positional argument:

Let’s delete a document whose f_name value is Nilesh.

Run:

from pymongo import MongoClient
mclient = MongoClient('localhost', 27017)
db = mclient['Newdatabase']
mcol = db["Employers"]

mquery = { "f_name": "Nilesh" }

mcol.delete_one(mquery)

for mrd in mcol.find():
  print(mrd)

mclient.close()

Output:

{'_id': ObjectId('6197f62a5312dd9e82102c6b'), 'f_name': 'Rob', 'l_name': 'Carpenter'}
{'_id': ObjectId('6197f7eb588c1566078fbff2'), 'f_name': 'Rob', 'l_name': 'Carpenter'}
{'_id': ObjectId('619800106f7cca5b81de7ec8'), 'f_name': 'Andy', 'l_name': 'Apple'}
{'_id': ObjectId('619800106f7cca5b81de7ec9'), 'f_name': 'AMad', 'l_name': 'Amhed'}
{'_id': 1, 'f_name': 'Nilesh', 'l_name': 'Mil'}
{'_id': 2, 'f_name': 'Andy', 'l_name': 'Apple'}
{'_id': 3, 'f_name': 'AMad', 'l_name': 'Amhed'}

As expected, the first found document with "f_name": "Nilesh"  is deleted and the later one is ignored.

delete_many() method:

The delete_many() function is used to remove multiple documents. It takes a mandatory parameter that specifies which document to be deleted.

Unlike the delete_one() function, the delete_many() erases all the matching documents if an input query returns multiple results.

Let’s delete the documents whose f_name value is Rob.

Run:

from pymongo import MongoClient
mclient = MongoClient('localhost', 27017)
db = mclient['Newdatabase']
mcol = db["Employers"]

mquery = { "f_name": "Rob" }

mcol.delete_many(mquery)

for mrd in mcol.find():
  print(mrd)

mclient.close()

Output:

{'_id': ObjectId('619800106f7cca5b81de7ec8'), 'f_name': 'Andy', 'l_name': 'Apple'}
{'_id': ObjectId('619800106f7cca5b81de7ec9'), 'f_name': 'AMad', 'l_name': 'Amhed'}
{'_id': 1, 'f_name': 'Nilesh', 'l_name': 'Mil'}
{'_id': 2, 'f_name': 'Andy', 'l_name': 'Apple'}
{'_id': 3, 'f_name': 'AMad', 'l_name': 'Amhed'}

We can see that documents with f_name with value Rob are all deleted from the database.

Delete all  documents:

To delete all the documents, you can pass an empty dict as a parameter to delete_many() function, it deletes all the documents from the database without having any condition.

from pymongo import MongoClient
mclient = MongoClient('localhost', 27017)
db = mclient['Newdatabase']
mcol = db["Employers"]

mcol.delete_many({})
print("Deletion of all documents successful!")
for mrd in mcol.find():
  print(mrd)

mclient.close()

Output:

Deletion of all documents successful!

Thanks for making it this far. We completed our CRUD operations on the MongoDB database using Python.

References:

Happy Learning 🙂

Leave A Comment