160

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

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
zer0stimulus
  • 22,306
  • 30
  • 110
  • 141
  • remove that app name from settings.py installed apps, then remove any link to that app from main urls.py file. Then u can safely delete that app – Aseem Jun 10 '19 at 20:48
  • If you're in development using SQLite and you don't mind resetting the database, is it okay to just remove the app folder and `db.sqlite3`, and remove it from `INSTALLED_APPS` in `settings.py`? – Kevin Aug 03 '20 at 19:39

7 Answers7

208
  1. Django < 1.7 has a handy management command that will give you the necessary SQL to drop all the tables for an app. See the sqlclear docs for more information. Basically, running ./manage.py sqlclear my_app_name gets you get the SQL statements that should be executed to get rid of all traces of the app in your DB. You still need to copy and paste (or pipe) those statements into your SQL client. For Django 1.7 and up, use ./manage.py migrate my_app_name zero (see the migrate docs), which runs the database cleaning automatically.

  2. 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.

  3. 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.

  4. (optional) If the app stored media files, cache files, or other temporary files somewhere, you may want to delete those as well. Also be wary of lingering session data that might be leftover from the app.

  5. (optional) I would also remove any stale content types.

Like so.

from django.contrib.contenttypes.models import ContentType
for c in ContentType.objects.all():
    if not c.model_class():
        print "deleting %s"%c # print(f"deleting {c}") # for Python 3.6+
        c.delete()
lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
Gabriel Hurley
  • 39,690
  • 13
  • 62
  • 88
  • 8
    Excellent answer, just one addition that I'd make: pip uninstall package-name is your friend, much nicer than trawling around your PYTHONPATH. – DaveJ Jul 26 '10 at 12:23
  • 31
    On Django 1.7, if you use migration, step 1 will be ```./manage.py migrate my_app_name zero```. And it will automatically execute the SQL as well. – Nathan Do Dec 20 '14 at 08:03
  • In this post http://www.marinamele.com/how-to-correctly-remove-a-model-or-app-in-django explain hot wo delete models and apps in Django. It's great together with this answer. – bgarcial Jan 19 '16 at 20:27
  • 15
    `sqlclear` was removed in Django 1.9 so this answer is only good for previous versions. See: https://docs.djangoproject.com/en/1.10/releases/1.9/#features-removed-in-1-9 – Akhorus Nov 24 '16 at 14:03
  • 12
    If the app being deleted has migrations, which other app's migrations depend on, the migration system will break down when it sees that those dependent migrations no longer exist. Be careful when using the above answer if the app to be deleted is a dependency in the migration history of another one of your apps. – Filip Kilibarda Oct 06 '17 at 19:08
  • 4
    And for completeness, you'll typically need to remove a reference to `include("appname.urls")` from the project's `urls.py` – nigel222 Jul 18 '19 at 16:14
  • 2
    I'm using Django 2.2, you can use `python manage.py remove_stale_contenttypes` for step 5 – Mustapha-Belkacim Nov 13 '19 at 08:41
26

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.

Alan W. Smith
  • 24,647
  • 4
  • 70
  • 96
guettli
  • 25,042
  • 81
  • 346
  • 663
  • 1
    Indeed, good answer on how to get this deployed when you have things automated – Rabarberski May 21 '21 at 13:24
  • Also beware that you will need to keep any functions/methods that your migrations depend on, e.g. if you use a function as [field default](https://docs.djangoproject.com/en/stable/ref/models/fields/#default). Migrations work with [historical models](https://docs.djangoproject.com/en/4.0/topics/migrations/#historical-models), which do not include custom model methods, etc. – djvg Aug 04 '22 at 15:30
  • 1
    This is really great (+1), enables migrations to just move forward. – Tiago Martins Peres Dec 12 '22 at 16:43
20
  1. comment out on settings.py in INSTALLED_APPS unnecessary app's line
  2. delete all folder __pycache__ and migrate at your project
  3. delete unnecessary model in models.py
  4. delete all import link in views.py, admin.py end etc.
  5. delete all link's in urls.py on your unnecessary app's
  6. in database delete unnecessary tables wich associated with the app (I do it with help program "Valentina Studio")
  7. delete app's folder
  8. in command line do it: python manage.py migrate and python manage.py syncdb
Vadim
  • 402
  • 6
  • 15
4

django app is a "set" of *.py files and a directory with a django-app-name. So you can simply delete the whole folder with all *.py files

To "remove" tables from DB you should use DELETE FROM <app-name_table-names>

Furthermore, you have to delete lines witgh app-name from setting.py in a root directory

V-Light
  • 3,065
  • 3
  • 25
  • 31
4

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
Shao
  • 121
  • 1
  • 4
2

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 :)

Muhammad Zubair
  • 466
  • 5
  • 17
0

These 3 steps below can completely remove an app from a Django project:

  1. Migrate an app with zero by specifying it as shown below. *This command below drops all the tables of an app from database:
python manage.py migrate <app_name> zero
  • <app_label> <migrationname>: Brings the ... app name. Use the name zero to migrate all the way back i.e. to revert all applied migrations for an app.

*Be careful, you cannot migrate multiple apps with zero by specifying them as shown below, then there is an error:

python manage.py migrate <app_name> <app_name> zero
  1. Remove the app from INSTALLED_APPS in settings.py as shown below:
# "settings.py"

INSTALLED_APPS = [
    # "my_app",
]
  1. Delete the app folder from a Django project.
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129