Django: How to completely uninstall a Django app?

Viewed 124117

What is the procedure for completely uninstalling a Django app, complete with database removal?

6 Answers

In my context the projects exists several times: I have a development system, some team mates have a development system, there is a staging system for the customer and a production system. This means I don't want to execute sql commands by hand. I want it to be automated.

Goal: Remove the app and all database tables.

Step 1: empty the app, but leave it installed

remove all files from the app, except the folder "migrations"

Execute this command:

python manage.py makemigrations -n drop_all_tables my_app_to_remove

The directory looks now like this:

my_app_to_remove/
my_app_to_remove/__init__.py
my_app_to_remove/migrations
my_app_to_remove/migrations/0001_initial.py
my_app_to_remove/migrations/....
my_app_to_remove/migrations/0030_drop_all_tables.py
my_app_to_remove/migrations/__init__.py

Leave my_app_to_remove in the file "settings.py".

Step 2: Deploy the changes

Update all projects. Tell the team mates to update their project and to run the migrations.

Step 3: remove "my_app_to_remove" from settings.py

Now remove "my_app_to_remove" from settings.py and deploy again.

As a first step, prevent any usage of the app models and deploy. This is essential for rolling deployment. Check notes below.

Then you have two options.

Option 1

  1. (Manual) Run python manage.py migrate <app_name> zero. This will revert all the migrations for the app and clean the django_migrations table
  2. Remove app (code, INSTALLED_APPS, urls.py, etc.)
  3. Deploy (python manage.py migrate)

Option 2

  1. Remove all models in the app
  2. Deploy
  3. Remove the app (code, INSTALLED_APPS, urls.py, etc.)
  4. Deploy
  5. (Manual) Clean django_migrations table

This answer is thinking in an automated rolling deployment context (e.g. Heroku). Manual means it normally cannot be done in automated deployment. Deploy basically refers to python manage.py migrate which is normally done in automated deployment.

Notes

  1. Make sure to remove code importing this app from other apps. Also any references in settings.py (but keep it in INSTALLED_APPS so we can run migrations), urls.py, etc.
  2. migrate <app_name> zero will revert all migrations depending on those migrations. So be careful if your other apps migrations depend on these migrations.
  3. Also be aware of cascade delete if you have not just schema migrations but also data migrations.
  4. Signal receivers receiving signals defined in other apps might be an issue in rolling deployment.

References

  1. https://docs.djangoproject.com/en/stable/ref/django-admin/#migrate

Safely Remove a Django app by Jordan Haines

To completely remove a Django app (with models), follow the steps below. Let’s pretend we’re removing an app called note_app:

  1. Search through your other apps for any note_app imports. The easiest way to do this is a project-wide search for “from note_app”. You may want to exclude the note_app directory from your search.
  2. Comment out all models in note_app.models. You may need to also remove entries from note_app.admin file if you registered your note_app models with Django admin.
  3. Look for and resolve errors when trying to run your Django app. P.S. You may have missed some model imports in step 1.
  4. Note that depending on how you define ForeignKey, OneToOne and ManyToMany fields, you may have missed some keys to your note_app models in step 1. It’s important that any fields that point to note_app models from models in other apps need to be deleted before you continue. Take a minute to make sure none of these fields were missed; if they were, then delete those fields that remain and create migrations that will remove the fields from your database.
  5. Run makemigrations. This should create a note_app migration that deletes all of the models you just commended out. This migration should also remove fields referring to note_app models from models in other apps.
  6. Run your migrations. You must run your migrations in all environments (including production) BEFORE you delete the app directory. The migrations that remove your app’s models from the database will be — surprise — in your note_app directory. If you delete the app directory prematurely, you will delete these migrations before they have a chance to clean up your database.
  7. You may get a notice that the content types for your deleted models are stale. When asked whether or not you want to delete these content types, reply “yes”
  8. Git Tip: Commit your migrations, take note of the commit, and then create a separate commit that deletes the note_app directory. When you are ready to apply your changes in a staging or production environment, checkout the commit you noted, run the migration, and then checkout the latest commit to delete the app directory. Your first commit should still have note_app in INSTALLED_APPS.
  9. Delete the directory that contains the note_app.
  10. Remove note_app from your INSTALLED_APPS setting.

And that’s really about it…super easy :)

Related