Skip to content

Instantly share code, notes, and snippets.

@jeromecc
Last active March 15, 2019 22:03
Show Gist options
  • Save jeromecc/7c12590a2d1983c4779bd8712511ed92 to your computer and use it in GitHub Desktop.
Save jeromecc/7c12590a2d1983c4779bd8712511ed92 to your computer and use it in GitHub Desktop.
Django 2.0 Delete database table after removing an app

First: Remove references in the code

remove app_to_remove from settings.INSTALLED_APPS
remove other references in urls.py or other places

Second: Clean the database

Create an empty migration for your django-project:

manage.py makemigrations your_django_project --empty

Edit the file. Here is a template:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('your_django_project', '0001_initial'),
    ]

    operations = [
        migrations.RunSQL('''
        drop table if exists app_to_remove_table1;
        drop table if exists  app_to_remove_table2;
        ....
        delete from auth_permission where content_type_id in (select id from django_content_type where app_label = '{app_label}');
        delete from django_admin_log where content_type_id in (select id from django_content_type where app_label = '{app_label}');
        # the following line points to a non existing table in my case
        #delete from reversion_version where content_type_id in (select id from django_content_type where app_label = '{app_label}');
        delete from django_content_type where app_label = '{app_label}';
        delete from django_migrations where app='{app_label}';
        '''.format(app_label='app_to_remove'))
    ]

Source: https://stackoverflow.com/questions/35745220/how-to-remove-an-app-from-a-django-projects-and-all-its-tables

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment