Reset Migrations in Django

Sometimes, you may need to reset migrations in Django. It is not a very big task but it is important when you are stuck somewhere and your database is not appropriate, you might need to edit or delete the migrations after you have created. Also, you might need to move to an older migration. So, this tutorial will be all about the solution to the above problem. If you want to reset migrations here are a few methods you can try to accomplish your task.

Method #1 – Reset Migrations

The very first method to delete all the databases is to first delete db.sqlite3 file and then delete all the migrations one by one. Then you can reset the migrations using:

python manage.py makemigrations
python manage.py migrate

This will reset your recent changes and delete the older ones.

Method #2

You can also use python manage.py flush to delete the database then we need to delete all the migrations manually. Then apply the above code to make new migrations.

Note: In both cases do not delete __init__.py files.

These methods are only applicable when you want to delete the migrations and remake them. You can also reverse the migrations if you want to, sometimes you might need to work with older migrations only. You can also accomplish this task if you want to. The below methods are for that purpose.

Method #3

When you want to reverse to some older migration you need to know the migration details at least the number of that particular migration. For example, if you wish to move to the older migration number 0005, you can write python manage.py your_appname migrate 0005.

python manage.py migrate your_appname 0005

Method #4

What if you wish to undo all the migrations in your database. You don’t want even a single migration in your database or in simple words you want to start from the beginning where no migrations exist.

Here’s a code to the above statement.

python manage.py migrate your_appname zero

The above four methods can be used to reset migrations. I hope these were helpful to you and solutions to the problem you might be facing.

Also read: Check the version of Django

Leave a Reply

Your email address will not be published.