How to delete / remove an app from a Django project?

Thomas Carbillet
3 min readFeb 2, 2023

--

Credits: Photo de Kim Gorga on Unsplash

Hello everybody,

The goal of this story is to help you completely remove an app from a Django project. It has been done with Django 3.2 (other versions might work) and PostgreSQL.

Requirements

  • A working Django project with at least one app containing at least one model installed.
  • An app you want to delete / remove.
  • Access to your production server shell.

Context

Let’s say we have an app named “quiz” that we want to remove from our project named my_project.

Here is what my_project architecture might look like before removing the quiz app:

my_project
|
└─── quiz
| | migrations
| | └─── 0001_initial.py
| | static
| | └─── quiz
| | | └─── example.css
| | | └─── hello.png
| | templates
| | └─── quiz
| | | └─── detail.html
| | | └─── list.html
| | __init__.py
| | admin.py
| | forms.py
| | models.py
| | urls.py
| | views.py
|
└─── my_project
| __init__.py
| settings.py
| urls.py
| wsgi.py

Instructions

This is an 8-step tutorial to completely remove the quiz app.

Step 1

First, remove all the references to the quiz app in the other apps of your project. Without this step, you might have import issues and break the project.

Step 2

Then, remove all the files and folders within the quiz app except the 2 files: __init__.py and models.py and the folder migrations.

You should now have something like this:

my_project
|
└─── quiz
| | migrations
| | └─── 0001_initial.py
| | __init__.py
| | models.py
|
└─── my_project
| __init__.py
| settings.py
| urls.py
| wsgi.py

Step 3

Next, you should remove all the code within quiz.models. You should have an empty models.py file at that point.

Step 4

Then, run the following commands in your shell on your local machine:

python manage.py makemigrations
python manage.py migrate

The first command will create a migration file that will delete all the models in quiz. Let’s say we had two models in “quiz”: Choice and Answer. The migration file might look like this:

# Generated by Django 3.2.16 on 2023-02-02 13:53

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("quiz", "0001_initial"),
]

operations = [
migrations.DeleteModel(
name="Choice",
),
migrations.DeleteModel(
name="Answer",
),
]

The second command will apply the migration to reflect the change in the database.

Step 5

Deploy your code to production and run the migrate command (python manage.py migrate) before continuing.

Step 6

On your local machine, you can now delete the folder quiz and the reference to quiz in INSTALLED_APPS in your settings.py. Here is the architecture of the project at that point:

my_project
|
└─── my_project
| __init__.py
| settings.py
| urls.py
| wsgi.py

Step 7

Deploy your code to production again.

Step 8

Remove the stale references of quiz by connecting to your production server and running the following admin command:

python manage.py remove_stale_contenttypes --include-stale-apps

A yes or no prompt will ask you if you want to remove the content type and permissions related to the quiz app. Type in “yes” and then “enter” to proceed. Here what the prompt will look like:

production-server:~$ python manage.py remove_stale_contenttypes --include-stale-apps
Some content types in your database are stale and can be deleted.
Any objects that depend on these content types will also be deleted.
The content types and dependent objects that would be deleted are:

- Content type for quiz.choice
- 4 auth.Permission object(s)
- 4 auth.Group_permissions object(s)
- Content type for quiz.quiz
- 4 auth.Permission object(s)
- 4 auth.Group_permissions object(s)
- 4 users.User_user_permissions object(s)

This list doesn't include any cascade deletions to data outside of Django's
models (uncommon).

Are you sure you want to delete these content types?
If you're unsure, answer 'no'.
Type 'yes' to continue, or 'no' to cancel: yes

👋 Thank you for reading this tutorial!

--

--