Deleting multiple rows from your database

There are 2 ways of doing this.

1 - Efficient..BUT..

MyModel.objects.filter(expired=True).delete()

This method, at the time of writing, does not call the delete function created in your Django models. If you do not have not created your own delete function, don't worry, its safe.

2 - Less efficient..BUT..

rows = MyModel.objects.filter(expired=True).delete()

for r in rows:
    r.delete()

This method does call your own delete function. There is obviously a bit more overhead due to requesting the rows, and deleting each row manually. However, if you have your own delete function, you probably want to run it.

Comments

There are no comments yet. Be the first.