I would like to know whats the cleanest way to remove all tables for a removed app using Django migrations. If for example I install a new package, I add the app to my settings.py and I do a manage.py makemigrations and a manage.py migrate; when then I decide that I don't want to use this package and I remove it from my settings.py, the command manage.py makemigrations will tell me "no changes detected" and so manage.py migrate will do nothing, but I need to remove the tables that this removed app had created.
I expected Django migrations to handle this so if I remove a app it would also create migrations to remove all the necesary tables.
To unapply migrations you should do the following: Use the python manage.py migrate your_app_name XXXX in case you want to unapply migrations after the XXXX migration. Otherwise use python manage.py migrate your_app_name zero to completely unapply all migrations.
To remove the app from your project, all you need to do is remove it from INSTALLED_APPS in your project's settings.py . Django will no longer load the app. If you no longer want the app's files hanging around, delete the app directory from your project directory or other location on your PYTHONPATH where it resides.
Deleting migration files means losing your history. This historical info is recorded in the django_migrations table in your database. if you delete migration files, you will get dependency errors. So Don't try to lose your history by deleting your migration files.
Django can't automatically generate data migrations for you, as it does with schema migrations, but it's not very hard to write them. Migration files in Django are made up of Operations, and the main operation you use for data migrations is RunPython .
you'd have to be careful with this one, make sure you understand the operations being reversed when you do it, but something like this should work:
manage.py migrate <app_name> zero
obviously you have to do this before removing it from your settings and such so migrations are discoverable.
edit: this has slowly been receiving a few upvotes - I figured I'd direct everybody towards the appropriate documentation, in particular:
Use the name zero to unapply all migrations for an app.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With