How to Reset Django Migrations: A Step-by-Step Guide with Data Backup

Oct. 3, 2023

3 min read

Resetting Django migrations can be a necessary step in your development process, especially when your database schema becomes cluttered, or you need to start fresh. However, it's essential to proceed with caution, as this process can result in data loss. In this guide, we'll walk you through the steps to reset Django migrations, complete with a detailed example.

Note: Before starting the migration reset process, it's crucial to backup your data. Use Django's dumpdata management command to create a backup of your data in a JSON or XML format:

python dumpdata > backup.json

Now, let's dive into the steps of resetting Django migrations.

Step 1: Delete Existing Migrations

The first step is to delete all the existing migration files in your Django app(s). Migration files are located in the migrations directory of each app. You can do this manually or by using the find command to delete them.

For instance, if you want to delete all migration files in an app called myapp, you can run:

cd myapp/migrations

rm *.py

rm *.pyc

This will remove all migration files in the migrations directory.

Step 2: Remove Migration History

After deleting the migration files, you should remove the migration history from the Django migrations table in your database. This table is usually named django_migrations. Access your database shell using the dbshell management command:

python dbshell

Inside the database shell, run SQL commands to delete records from the django_migrations table for your app:

DELETE FROM django_migrations WHERE app='myapp';

Replace 'myapp' with the name of your app.

Step 3: Create Initial Migrations

With the existing migrations removed, you need to create new initial migrations for your app(s) using the makemigrations management command:

python makemigrations

This command generates new initial migration files for your app(s) based on the current state of your models.

Step 4: Apply Migrations

Now, apply the new initial migrations to recreate the database schema:

python migrate

This command will create the tables and schema based on the newly generated migration files.

Step 5: Restore Data

If you backed up your data in Step 1, you can now restore it using the loaddata management command:

python loaddata backup.json

Replace backup.json with the actual filename of your backup.


Resetting Django migrations can be a powerful tool for managing your database schema, but it should be used with caution. Always remember to back up your data before proceeding, especially in production environments. After resetting migrations, thoroughly test your application to ensure it works as expected with the new migrations and the restored data.

By following these steps, you can successfully reset Django migrations and maintain a clean and organized database schema for your Django project.

django database db migrations Appreciate you stopping by my post! 😊

Add a comment

Note: If you use these tags, write your text inside the HTML tag.
Login Required