How to properly remove Django migrations with MySQL shell
Rocktim Saikia/2 min read
When you want to remove all the migration records of a specific app in Django, you can use the following steps to do it properly. Many developers just delete the migration files in the migrations
directory and run python manage.py migrate app_name zero
to reset the migrations. However, this is not the best practice because it will leave the migration records in the django_migrations
table in the database. This can cause issues when you want to create new migrations for the app in the future.
- Select your target database
USE my_database;
- Show all tables in the database
SHOW TABLES;
- See all the rows in the
django_migrations
table
SELECT * FROM django_migrations;
- Get migration records of the target
app
SELECT * FROM django_migrations WHERE app='app_name';
- Delete the target migration records
Delete all migration records of the target app after a specific migration id
DELETE FROM django_migrations WHERE app='app_name' AND id>300;
or
Delete a specific migration record of the target app selected by the
name
field
DELETE FROM django_migrations WHERE app='app_name' AND name='0001_initial';
- After the above steps, you can safely remove the migration files in your apps
migrations
directory.