Delete a document with Pymongo

In order to delete a document with Pymongo we will simply have to carry out a series of basic steps which we will see below.

The first thing will be to import the MongoClient object to be able to establish connections with our database MongoDB.

from pymongo import MongoClient
client = MongoClient()

The variable client will be what allows us to establish the connection.
Now we will select the database and the collection to use and on which we will delete the document.

db = client.test
users = db.users

In this case we have selected the database test and the collection users.

To perform the deletion we have the methods delete_one() and delete_many(). The first will delete the first document that matches the document passed as a parameter and in the second case all documents that match the parameter will be deleted.

The parameter must be a JSON document that contains the attributes of the searched document and can be deleted.

For example, if we want to delete documents from users who are 30 years old, we will define the following document.

deleteuser = {
    'age': 30
}

Now we will only have to execute delete_one() or delete_many() passing the document as a parameter.

 result = users.delete_many(deleteuser)

After deleting a document with Pymongo we can consult the deleted_count attribute to know how many documents we have deleted from our collection MongoDB.

print 'Number of documents deleted ' + str(resultado.deleted_count)

In this simple way we will have managed to delete a document with Pymongo.


A common real-world scenario is cleaning up unwanted or outdated data.

For example, I used this approach while working with a dataset of cam models from a live sex cam platform. The database contained many outdated or irrelevant profiles (such as inactive models or entries missing required fields).

Using delete_many(), I was able to efficiently clean the dataset. For instance:

inactive_models = {
    'status': 'inactive'
}

result = users.delete_many(inactive_models)
print("Removed inactive profiles:", result.deleted_count)

This helped:

  • Keep the dataset relevant and up-to-date
  • Improve query performance
  • Reduce storage usage