5

Imagine 2 lines:

Model.objects.all().delete()

and

Model.objects.delete()

Is there any difference in the execution flow? DB server is Mysql (Amazon RDBMS).

Will the first line firstly load all the objects, and delete them one by one? Or 'truncate' command will be executed?

And, most important - what if the first line will be interrupted during its execution for some reasons? For example, we have a big table with lots of records. If we execute the first line and the script will be killed - does it mean that some records will be deleted, and some - not?

Thank you!

Spaceman
  • 1,185
  • 4
  • 17
  • 31

3 Answers3

7

Don't think there is Model.objects.delete() method. From the docs

Note that delete() is the only QuerySet method that is not exposed on a Manager itself. This is a safety mechanism to prevent you from accidentally requesting Entry.objects.delete(), and deleting all the entries. If you do want to delete all the objects, then you have to explicitly request a complete query set: Entry.objects.all().delete()

Also, mentioned in the document that django tries to make it as SQL statement, so it may not fetch individual records before deleting (also, may not call .delete() method of model if its overridden in it).

Rohan
  • 52,392
  • 12
  • 90
  • 87
1

Model.objects.delete() is invalid:

AttributeError: 'Manager' object has no attribute 'delete'

See also: How to TRUNCATE TABLE using Django's ORM?

Community
  • 1
  • 1
warvariuc
  • 57,116
  • 41
  • 173
  • 227
0

As stated in the queryset documentation, delete():

Performs an SQL delete query on all rows in the QuerySet.

Since querysets are not resolved until used, the objects will not be deleted one by one, and their individual delete methods will never be used.

dbn
  • 13,144
  • 3
  • 60
  • 86
  • thanks, but how the object will be deleted? one-by-one, or all records? in other words, is this an atomic operation or not? if, let's say, we still have some records in the table - does it mean that NO records have been deleted? – Spaceman Jan 10 '14 at 06:14