Reputation: 1239
I have a third-party application in my Django project that I'd like to remove. My problem is that if I simply remove it from INSTALLED_APPS
, all the models and associated tables that it created in my database will be kept.
I know I can manually DROP thoses tables afterwards but I want this to be automated (since there are multiple running instances of this project, I don't want to ask people to run any manual command when upgrading). I'm wondering if there is a recommended or "clean" way to do so with Django migrations.
I created a migration file in one of my apps (the one that used this third-party app) with a migrations.DeleteModel(name='ThirdPartyModel')
but it fails to execute because this model is managed by the other app.
What is left it to write a RunSQL migration whith a DROP TABLE thirdpartyapp_thirdpartymodel
and a DELETE FROM django_content_type WHERE app_label = thirdpartyapp AND model = thirdpartymodel
.
Is there any other option? How are these types of operations supposed to be done?
I'm using Django 3.0.
Upvotes: 2
Views: 1532
Reputation: 21807
First make sure none of your models have any dependency to this third party app, so no foreign keys, one to one fields, many to many relations, etc. Remove any of these and migrate.
Next you want to migrate the changes this app made to zero
so you can run the following command:
python manage.py migrate <third_party_appname> zero
Referred from the documentation on migrate
[Django docs]:
<app_label> <migrationname>: Brings the database schema to a state where the named migration is applied, but no later migrations in the same app are applied. This may involve unapplying migrations if you have previously migrated past the named migration. You can use a prefix of the migration name, e.g. 0001, as long as it’s unique for the given app name. Use the name zero to migrate all the way back i.e. to revert all applied migrations for an app.
This will revert all changes made to the database and now you can freely remove it from INSTALLED_APPS
.
Upvotes: 1