Reputation: 23606
What is the procedure for completely uninstalling a Django app, complete with database removal?
Upvotes: 164
Views: 152779
Reputation: 389
Prior answers are mostly variations on: https://patrick.cloke.us/posts/2020/01/23/cleanly-removing-a-django-app-with-models/
Django now documents a "sanctioned" method for deleting an app: https://docs.djangoproject.com/en/dev/howto/delete-app/
Combined with script below, this solution...
This process is completed in 2 phases.
Phase 1: delete app models, migrate, and deploy...
Phase 2: remove all traces from DB and app package from code base at some future date...
A complete management command for Step 2:
https://gist.github.com/powderflask/f0ae8b9ae6c7bb471f591de5175204cd
The heart of that command:
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import Permission
from django.contrib.admin.models import LogEntry
from django.core.management import call_command
app_label = 'APP_LABEL_TO_DELETE' # PARAMETER: the app to be removed
content_types = ContentType.objects.filter(app_label=app_label).all()
permissions = Permission.objects.filter(content_type_id__in=content_types)
log_entries = LogEntry.objects.filter(content_type_id__in=content_types)
# reverse migrations to remove model DB tables and migrations entries
call_command('migrate', app_label, 'zero')
# delete the content type related data
log_entries.delete()
permissions.delete()
content_types.delete()
Upvotes: 0
Reputation: 1
These 3 steps below can completely remove an app from a Django project:
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
INSTALLED_APPS
in settings.py
as shown below:# "settings.py"
INSTALLED_APPS = [
# "my_app",
]
Upvotes: 0
Reputation: 140
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
python manage.py migrate <app_name> zero
. This will revert all the migrations for the app and clean the django_migrations
tableINSTALLED_APPS
, urls.py
, etc.)python manage.py migrate
)Option 2
INSTALLED_APPS
, urls.py
, etc.)django_migrations
tableThis 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
settings.py
(but keep it in INSTALLED_APPS
so we can run migrations), urls.py
, etc.migrate <app_name> zero
will revert all migrations depending on those migrations. So be careful if your other apps migrations depend on these migrations.References
Upvotes: 5
Reputation: 574
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
:
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.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.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.note_app
from your INSTALLED_APPS
setting.And that’s really about it…super easy :)
Upvotes: 4
Reputation: 27816
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.
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".
Update all projects. Tell the team mates to update their project and to run the migrations.
Now remove "my_app_to_remove" from settings.py and deploy again.
Upvotes: 28
Reputation: 40052
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.
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.
(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.
(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()
Upvotes: 213
Reputation: 464
settings.py
in INSTALLED_APPS
unnecessary app's line__pycache__
and migrate
at your project models.py
views.py
, admin.py
end etc. urls.py
on your unnecessary app'spython manage.py migrate
and python manage.py syncdb
Upvotes: 20
Reputation: 3115
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
Upvotes: 4