HOWTO · Django

How to Reset Migrations in Django

Choose between rolling back one Django app, unapplying its migrations, clearing data with `flush`, or rebuilding a disposable development database. Learn what each operation changes and how to reduce data-loss and migration-history risks.

On this page

Django migrations are versioned instructions for changing a database schema. Django also records which migrations have been applied in the database. “Reset migrations” can therefore mean several different things: move one app backward to a known migration, unapply that app’s migrations, clear rows while keeping the schema, or replace a development database. Choose the operation by the result you need; deleting migration files is not a substitute for any of them.

Before a destructive operation, confirm which settings file and database your command will use, check the current migration state with python manage.py showmigrations, and make a restorable backup when the data matters. The commands below use AppName as a placeholder for the app label, such as blog; use the label Django recognizes, not necessarily the Python package’s full path.

What a migration reset changes

A migration file describes schema operations and may also move or transform data. The migration table records applied migration names. Rolling an app back changes its recorded schema state by running reverse operations where available; it does not rewrite the migration files. Clearing rows with flush leaves the schema and migration history in place. Replacing a development database removes its data and its migration records, after which migrations must be applied again.

These are different scopes. If you need to undo a recent schema change, target an earlier migration. If the app should have no applied migrations, target zero. If you only want an empty set of application data, use flush. If a local database is disposable and you want a clean rebuild, replace that database and migrate the project again. Do not delete migration files to make the database appear reset: existing environments and teammates still depend on the files that define the shared migration history.

Roll back one app to an earlier migration

Find the target migration with showmigrations AppName or inspect that app’s migration files. Then use python manage.py migrate AppName 0004 to move the app to migration 0004; replace both placeholders with the actual app label and target. A migration name such as initial can be used instead of a number when that is the name shown for the migration.

The target is the state after that migration, so later migrations for that app are reversed. Reverse operations may alter or remove columns and can delete data stored in them. Django may also need to adjust dependent migrations to keep the migration graph consistent. Review the planned changes and the database selected by the active settings before running the command, especially when other apps depend on the target app.

A small Python snippet can make the command template explicit before you adapt it to your project. Replace the sample app and target with values from your own migration list.

Run this example:

app_label = "blog"
target_migration = "0004"
print(f"python manage.py migrate {app_label} {target_migration}")
python manage.py migrate blog 0004

This prints a command template; it does not connect to Django or change a database.

Reset one app to zero

To unapply all migrations for one app, use python manage.py migrate AppName zero. This affects that app’s migration state and reverses its schema operations; it is not a database-wide reset. Depending on the migrations and relationships involved, reversing them can remove tables or data and may require dependent migrations to be unapplied as well. Inspect the proposed effects in a disposable environment first when the result is uncertain.

Do not follow this with deletion of the app’s migration files in a shared or deployed project. The files are the history Django uses to build the schema on other databases. If a team is intentionally squashing or replacing migration history, coordinate a separate migration-maintenance procedure across every environment rather than treating zero as permission to erase that history.

Reset a development database

For an empty database while keeping its tables and applied migrations, run python manage.py flush. It deletes data from the database and resets sequences, but keeps the schema and migration records. It is usually the right choice when you want to reload fixtures or start with empty application data; it does not rebuild the schema from migration files.

For a complete local rebuild, stop processes using the database, confirm that the active settings point to a disposable development database, and replace that database using the procedure for its engine. With SQLite this can mean removing the local database file; with a server database it typically means dropping and recreating a development database with appropriate credentials. Then run python manage.py migrate to create the schema and apply the project’s migrations. Database deletion commands differ by engine, so use that database’s documented procedure and never copy a development reset command to production without an explicit recovery plan.

Removing migration files and creating new ones from current models is a separate, coordinated history rewrite. It can break deployed installations, teammates’ working copies, and migration dependencies even if a local database was rebuilt successfully. Keep migration files under version control and use new migrations for ordinary model changes.

Handle irreversible migrations safely

Some migrations cannot be reversed because their operation discards information or has no defined reverse operation. Django can stop a rollback with IrreversibleError. The database’s state after failure depends on the migration’s atomicity and the database’s transaction behavior; a rollback may have partial effects. Do not assume that a rollback command will restore deleted or transformed data.

Before attempting a rollback, review the migration operations and back up data that must survive. Test the rollback against a recent database copy, verify the resulting schema and important records, and confirm the target environment again immediately before applying it. If the migration is irreversible, restore from a suitable backup or write a deliberate forward migration that reaches the desired state; do not delete migration records manually to conceal the mismatch.