Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to drop all tables from the database with manage.py CLI in Django?

How can I drop all tables from a database using manage.py and command line? Is there any way to do that executing manage.py with appropriate parameters so I can execute it from a .NET application?

like image 728
kmalmur Avatar asked Aug 05 '10 11:08

kmalmur


People also ask

How do I drop all tables in Django?

There's no native Django management command to drop all tables. Both sqlclear and reset require an app name. However, you can install Django Extensions which gives you manage.py reset_db , which does exactly what you want (and gives you access to many more useful management commands).

How do I drop all the tables?

Select all of the tables in your database in the Schema Browser clicking on the first table, holding Shift, and clicking on the last table. Right-click on the selected tables and select “Drop (n) Tables…”


1 Answers

As far as I know there is no management command to drop all tables. If you don't mind hacking Python you can write your own custom command to do that. You may find the sqlclear option interesting. Documentation says that ./manage.py sqlclear Prints the DROP TABLE SQL statements for the given app name(s).

Update: Shamelessly appropriating @Mike DeSimone's comment below this answer to give a complete answer.

./manage.py sqlclear | ./manage.py dbshell 

As of django 1.9 it's now ./manage.py sqlflush

like image 177
Manoj Govindan Avatar answered Sep 21 '22 23:09

Manoj Govindan