How to TRUNCATE TABLE using Django's ORM?

Silver Light picture Silver Light · Jun 7, 2010 · Viewed 41.4k times · Source

To empty a database table, I use this SQL Query:

TRUNCATE TABLE `books`

How to I truncate a table using Django's models and ORM?

I've tried this, but it doesn't work:

Book.objects.truncate()

Answer

Ned Batchelder picture Ned Batchelder · Jun 7, 2010

The closest you'll get with the ORM is Book.objects.all().delete().

There are differences though: truncate will likely be faster, but the ORM will also chase down foreign key references and delete objects in other tables.