Reputation: 949
I trying to delete over 20,000,000 objects at once by executing:
MyModel.objects.filter(some_field__lt=100).delete()
But I can't, because of not enough memory (1GB VPS). Is it bad method or what? How to do it?
Upvotes: 2
Views: 1091
Reputation: 3631
In this case I think it's better to use raw sql query https://docs.djangoproject.com/en/dev/topics/db/sql/#executing-custom-sql-directly
cursor.execute("DELETE FROM mymodel WHERE some_field < %s", [value])
Upvotes: 4
Reputation: 2958
Try limiting what is being selected MyModel.objects.filter(some_field__lt=100).only('id').delete()
or if needed get the query and perform the query outside of django
MyModel.objects.filter(some_field__lt=100).query
Upvotes: 2