How to properly remove Django migrations with MySQL shell

Hero image
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.

  1. Select your target database
USE my_database;
  1. Show all tables in the database
SHOW TABLES;
  1. See all the rows in the django_migrations table
SELECT * FROM django_migrations;
  1. Get migration records of the target app
SELECT * FROM django_migrations WHERE app='app_name';
  1. 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';
  1. After the above steps, you can safely remove the migration files in your apps migrations directory.