3

As title suggests, I want to remove all contents of the database. I'm making an application in Python using Flask and MongoAlchemy for database. I have the class People which models the documents. The structure is:

class People(db.Document):
    Name = db.StringField()
    Age = db.IntField()
    Password = db.StringField()
    Vms = db.AnythingField()

With the remove method I can delete only one document at once, based on the name or other detail, but I want to be able to delete them all at once.

This is my function for deleting a document, based on name.

def delete_document(name):
    x = People.query.filter(People.Name == name).first()
    x.remove()

Where People is the reference to the model class.

1 Answers1

1

Apparently there is a easy way, using one of the built-in methods from Query class, remove_all. With the remove_all method you can get all the results from database in a list, and after that you can call the remove method on every instance. The code is posted below. Not sure if there is a more efficient way of doing this, so feel free to post it.

def deleteAll():
    x = People.query.all()
    for i in range(0, len(x)):
        x[i].remove()