35

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

Community
  • 1
  • 1
bharal
  • 15,461
  • 36
  • 117
  • 195
  • 1
    Check this out - http://stackoverflow.com/questions/3414247/django-drop-all-tables-from-database – zubinmehta May 15 '12 at 17:40
  • Strange order: In step A you delete all tables. In step B you want to delete all data in all tables. But there is not single table since all tables where deleted in step A. I don't get it :-) – guettli Sep 26 '15 at 16:30
  • 1
    @guettli ha ha! I think the OP did not consider any meaningful ordering, he just wanted to know them all. – Sнаđошƒаӽ Dec 29 '15 at 16:17

7 Answers7

39

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.

Md Sifatul Islam
  • 846
  • 10
  • 28
nates
  • 8,312
  • 5
  • 32
  • 28
  • 4
    The problem with this is that `manage.py sqlclear` by itself won't work. You'll have to pass it each app name individually, and if you use `syncdb`, it won't reflect any changes in existing models (which is why south is recommended). – Burhan Khalid May 15 '12 at 19:44
  • eh, for my own notes "reset" will clear the entire db (drop tables etc) – bharal May 18 '12 at 00:56
  • 3
    `syncdb` is deprecated in favour of `migrate`, since Django 1.7. – Jonathan Hartley Oct 12 '15 at 12:17
  • Link to south docs currently 404'd (http://south.aeracode.org/docs/about.html) – There Jan 27 '21 at 17:50
14

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

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
11

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.

Anuj Gupta
  • 10,056
  • 3
  • 28
  • 32
4

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

bento
  • 4,846
  • 8
  • 41
  • 59
4

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
delehef
  • 681
  • 4
  • 7
2

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.

Reimus Klinsman
  • 898
  • 2
  • 12
  • 23
0

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

Community
  • 1
  • 1
zubinmehta
  • 4,368
  • 7
  • 33
  • 51