What is the django command to delete all tables?

Are there django commands that

A. Delete all tables

B. delete all data in all tables

C. Create all tables as defined in the model?

I cannot find these right now!

And by commands i mean those little things that are like

runserver

etc

Answers


A. Delete all tables

manage.py sqlclear will print the sql statement to drop all tables

B. delete all data in all tables

manage.py flush returns the database to the state it was in immediately after syncdb was executed

C. Create all tables as defined in the model?

manage.py syncdb Creates the database tables for all apps in INSTALLED_APPS whose tables have not already been created.

See this page for a reference of all commands: https://docs.djangoproject.com/en/dev/ref/django-admin/

But you should definitely look into using south as someone already mentioned. It's the best way to manage your database.

N.B: syncdb is deprecated in favour of migrate, since Django 1.7.


If you have the client libraries installed for your database, you can run this:

python manage.py sqlflush | python manage.py dbshell

This doesn't drop the tables, but truncates them.

There isn't a command that does the it all in one go, but this "one liner" will drop all the tables and then re-create them. It would only work if you were running on a system that provides these utilities at the shell.

echo 'from django.conf import settings; print settings.INSTALLED_APPS; quit();' | python manage.py shell --plain 2>&1 | tail -n1 | sed -r "s|^.*\((.*)\).*$|\1|; s|[',]| |g; s|django\.contrib\.||g" | xargs python manage.py sqlclear | python manage.py dbshell && python manage.py syncdb


Neither manage.py sqlclear nor manage.py reset is capable of dropping all tables at once, both require an appname parameter.

You should take a look at Django Extensions, it gives you access to manage.py reset_db as well as many other useful management commands.


I recommend using django-south. It allows you to sync your models to your database not just when you add a field, but also when you delete a field/model. I really find it to be an essential component of building a Django site. Once installed, you could run a command like:

./manage.py migrate app zero

You can learn more here: http://south.aeracode.org/docs/about.html


And a simpler oneliner to drop all the tables for django 1.5+:

python2 manage.py sqlflush | sed 's/TRUNCATE/DROP TABLE/g'| python2 manage.py dbshell

I was able todrop my tables. Run

python manage.py sqlclear appname

Take note of the commands given. Then run

python manage.py dbshell

Add the commands given in the first step. line by line. When you are done, type '.exit' (for SQlite3). Resync your DB and you should be good to go. To be sure, check the tables with:

python manage.py shell
>>> from yourapp import yourclasses
>>> yourviews.objects.all()

it should return a []. I hope this helps.


A implies B, right?

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

For C,

python manage.py syncdb

Ofcourse for smart data migration I go with what @bento mentioned: django-south


Need Your Help

How to protect against diacritics such as Zalgo text

c# unicode user-input diacritics zalgo

The character pictured above was tweeted a few months ago by Mikko Hyppönen, a computer security expert known for his work on computer viruses and TED talks on computer security. In respect for SO, I