The Django migration system was developed and optmized to work with large number of migrations. Generally you shouldn’t
mind to keep a big amount of models migrations in your code base. Even though sometimes it causes some undesired
effects, like consuming much time while running the tests. But in scenarios like this you can easily disable the
migrations (although there is no built-in option for that at the moment).
Anyway, if you want to perform a clean-up, I will present a few options in this tutorial.
Scenario 1:
The project is still in the development environment and you want to perform a full clean up. You don’t mind
throwing the whole database away.
1. Remove the all migrations files within your project
Go through each of your projects apps migration folder and remove everything inside, except the __init__.py file.
Or if you are using a unix-like OS you can run the following script (inside your project dir):
2. Drop the current database, or delete the db.sqlite3 if it is your case.
3. Create the initial migrations and generate the database schema:
And you are good to go.
Scenario 2:
You want to clear all the migration history but you want to keep the existing database.
1. Make sure your models fits the current database schema
The easiest way to do it is trying to create new migrations:
If there are any pending migration, apply them first.
If you see the message:
You are good to go.
2. Clear the migration history for each app
Now you will need to clear the migration history app by app.
First run the showmigrations command so we can keep track of what is going on:
Result:
Clear the migration history (please note that core is the name of my app):
The result will be something like this:
Now run the command showmigrations again:
Result:
You must do that for all the apps you want to reset the migration history.
3. Remove the actual migration files.
Go through each of your projects apps migration folder and remove everything inside, except for the __init__.py file.
Or if you are using a unix-like OS you can run the following script (inside your project dir):
PS: The example above will remove all the migrations file inside your project.
Run the showmigrations again:
Result:
4. Create the initial migrations
Result:
5. Fake the initial migration
In this case you won’t be able to apply the initial migration because the database table already exists. What we want
to do is to fake this migration instead: