Removing third party apps from the Django database
Use the "zero" migrations to remove a third party app from your Django database.
Adding third-party apps to Django is easier than removing them. If the app includes models (and they usually do), then you can't forget to remove the data from the database when you uninstall it! If you don't, you're either going to end up with orphaned data or, even worse, you'll run into some foreign-key constraints or something down the road when a certain type of query tries to execute.
The command you need to know here is:
# (Terminal)
python manage.py migrate <app> zero
The "zero" part is the key. This rolls back all migrations for the app, and ultimately removes it from your database.
Note: Make sure you get all of the migration "apps" related to the package. Some packages, like django-allauth, use more than one app namespace.
The trick here is the order of operations — you need to delete the data while the app is still installed in order for the migrations to be found.
In development you can run the migrate <app> zero
command and then immediately update your settings.py
.
But in production it's not quite that simple.
In production, there's going to be "downtime" and the steps look more like this:
- Put your app in maintenance mode (Heroku example)
- Make sure you have a database backup!
- Run the
migrate <app> zero
command in production (ex.heroku run python manage.py migrate <app> zero
) - In development, remove the app from
INSTALLED_APPS
in yoursettings.py
and commit the change (you can do this first if you remember not to deploy it until the right time) - Deploy
- Turn off maintenance mode
That should do it! Make sure you try it out in development (and staging, if you have it) and know the steps you're going to go through. You don't want to get halfway through the process on your production database and make a mistake that adds even more steps to the process.