Search code examples
djangopython-3.xdjango-modelsdjango-migrations

Django: Safely Remove Old Migrations?


I've got a Django app with a lot of out-of-date migrations. I'd like to remove the old migrations and start fresh.

The app has 14 different "migrations" folders.

Here is what a few of them look like:

enter image description here

enter image description here

enter image description here

Is it safe to remove all the contents from each of these folders? Or, do I have to make sure to only remove some of the files -- and if so which files?


Solution

  • You should never just delete migrations before unapplying them, or it will be a nightmare when you want to apply new migrations.

    To unapply migrations you should do the following:

    1. Use the python manage.py migrate your_app_name XXXX in case you want to unapply migrations after the XXXX migration. Otherwise use python manage.py migrate your_app_name zero to completely unapply all migrations.

    2. Remove the .pyc files under /migrations/_pycache_/ that you have unapplied.

    3. Remove the .py files under migrations/ that you have unapplied.

    Now you can create new migrations without any headaches.

    If what you're looking for is to squash all the migrations into one, do the steps above removing all migrations and then run python manage.py makemigrations your_app_name to create a single migration file. After that just run python manage.py migrate your_app_name and you're done.