How to Reset the Database in Django?

As one of the most used frameworks for building web applications, Django provides us with powerful tools to make our development easier and faster. One of the standard tools of Django web app development is the SQLite3 database. This article will show you how to reset a given database.

Why would You want to reset the database in Django?

In the early development stages, when there is no real data in the database, many database changes can happen. Often it’s easier to reset the database than to write custom migration files.

First, our SQLite3 database is created by the given parameters in settings.py. It defaults:

DATABASES = {
     'default': {
         'ENGINE': 'django.db.backends.sqlite3',
         'NAME': BASE_DIR / 'db.sqlite3',
     }
}

And db.sqlite3 database is created locally when we apply migrations – python manage.py migrate.

Considering that, I’ll show you a few ways to reset your database in Django:

  • Manually resetting
  • Using Django commands

Reset database in Django manually

Manually resetting the database requires basic knowledge of project structure and its files.

SQLite3 database requires:

  • migrations files
  • db.sqlite3 file

Resetting database in Django consists of 4 steps:

  1. Delete all migrations files
  2. Delete db.sqlite3 file
  3. Make new migrations filespython manage.py makemigrations
  4. Migrate changespython manage.py migrate

Certainly, you could use .sh scripts to automatize given steps.

In the following example, we have a project containing two apps, authapp, and bookapp. This script automizes the above steps.

rm authapp/migrations/00*.py
rm bookapp/migrations/00*.py
rm db.sqlite3

python3 manage.py makemigrations authapp
python3 manage.py makemigrations bookapp
python3 manage.py migrate

First, we delete all migrations files from both applications, and then we delete db.sqlite3 database. In case of new changes, we run makemigrations and then migrate changes, creating a new db.sqlite3 database.

WARNING - Deleting migration files is bad practice. However, making new migrations and migrating changes is still a better choice than resetting your database.

Reset database in Django using commands

Flush method

Another option that does exactly the same is python manage.py flush.

>>>python manage.py flush

You have requested a flush of the database.
This will IRREVERSIBLY DESTROY all data currently in the "db.sqlite3" database,
and return each table to an empty state.
Are you sure you want to do this?

Type 'yes' to continue, or 'no' to cancel: yes

python manage.py flush deletes all data in all tables in the database. It’s important to know that flush doesn’t migrate new changes to the database; it only deletes all data. So if you changed your models, you still need to makemigrations and then migrate them to the database.

Migrate zero method

python manage.py migrate <app_name> zero method is more specifically connected to Django apps. It can revert all migrations related to a specific application in Django.

How can this exactly help us reset the database?

Well, in pair with new migration, it rebuilds models from a given app, and therefore, we get new tables without old data.

For example, if we want to reset all data from <example_app>, we need to take the following steps:

  1. python manage.py migrate example_app zero
  2. python manage.py migrate example_app

Reset non-local database in Django

Databases come in all types and forms, so making a universal way to reset them is generally challenging. Both flush and migrate zero methods should work. Still, in the worst-case scenario, you can always delete the database and migrate changes to a new one.

NOTE - If your database contains sensitive data, consider using dumpdata and/or database backups

Additionally, you can always check official Django documentation.

Summary

I always find it challenging and tiresome to handle database changes in the early stages of development when data is not important. The early stages include many changes in client requirements and, therefore, a lot of the database changes. That’s why I choose to write this article and share my experiences (and scripts) to help you overcome these obstacles.

Hope you are doing fine and enjoy! 🙂