Guide 2023 By davegaeddert

Removing third party apps from the 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:

  1. Put your app in maintenance mode (Heroku example)
  2. Make sure you have a database backup!
  3. Run the migrate <app> zero command in production (ex. heroku run python manage.py migrate <app> zero)
  4. In development, remove the app from INSTALLED_APPS in your settings.py and commit the change (you can do this first if you remember not to deploy it until the right time)
  5. Deploy
  6. 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.

Feedback

Questions or ideas for improvement? Open a GitHub Discussion →