18

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?

VikR
  • 4,818
  • 8
  • 51
  • 96
  • If you've not created some custom migrations for like loading data, then yes. It should be safe to remove all of the migrations in the migrations folder and run `makemigratons` command. You can always copy old migrations for backup unless you store them under version control like git. Then manual backup is not needed ofc. – Nf4r Sep 18 '19 at 21:11
  • Is it safe to remove the files `_init_.py`, `collection.json`, `index.js`, etc.? – VikR Sep 18 '19 at 21:25

6 Answers6

31

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.

Josh Correia
  • 3,807
  • 3
  • 33
  • 50
Jordan Mora
  • 889
  • 6
  • 16
  • I'm still something of a newbie at Django migrations. I don't want to unapply any migrations. All the created migrations have been applied on my local dev system, and everything is working correctly on dev. The production db is up-to-date with the dev db, so I don't need to apply the migrations to production. That's why I'm thinking it might be helpful to delete the old migration files prior to pushing to production. Does that make sense? – VikR Sep 18 '19 at 21:32
  • Django stores the newest migrations information in the database. Thus if you remove now all of the current migrations and create new one (`0001_initial.py`), once you run `manage.py migrate` on production database you will get yourself into troubles. You can only remove migrations if you can drop the whole database and load the data manually from backup after running fresh migrations. – Nf4r Sep 18 '19 at 21:36
  • In that case it is not safe to remove migrations, since you have it in production and your database is probably populated with data. If you do it, all your data will be lost. Like the Beatles said, just let it be. – Jordan Mora Sep 18 '19 at 21:40
  • If you delete your migrations, there's no way for you to reproduce the current production setup. Image you break your local DB. There's no way to go back, or figure out what's running in production (short of jumping into the production database and analysing the schema manually). Migrations are there to help you reach the same DB state again. – WhyNotHugo Jul 11 '20 at 12:18
  • @JordanM. I am running into errors in production after deleting my dev database, please recommend what I'm doing wrong https://stackoverflow.com/questions/63111620/does-deleting-migrations-and-database-in-dev-cause-issues-when-pushing-to-produ – Padoga Jul 27 '20 at 10:23
  • I know this answer is old but I wonder if I can edit the module name in the database manually instead of re-migrating the data until I don't remove the data in my own database? – Abdelhamed Abdin Aug 05 '20 at 21:20
  • @JordanMora I recently know that I have to delete the ".pyc" , I haven't been doing that, would it be fine if I just delete them? – ResponsibleTrain8 Apr 16 '23 at 22:16
  • 1
    Hey @ResponsibleTrain8, yes you can delete them. The deletion is to erase all trace of the migration you want to unapply. These files are auto-generated by the Python interpreter to improve performance, If you remove them, they'll eventually appear again. – Jordan Mora Apr 17 '23 at 16:50
7

That depends. If you have a production database (or any database you cannot simply drop and recreate), then the answer is no, you cannot safely remove migrations.

If you do not have any permanent databases, then yes, you can remove all migrations, run python manage.py makemigrations --initial and it will create fresh migrations based on your current models.

Also, you should check if any of the migrations are custom data migrations written by hand. If there are any, you might want to keep those.

The .pyc files are generally safe to remove, provided the related .py files are still there.

your first screenshot is not Django and looks like a JS project of some sort.

Mad Wombat
  • 14,490
  • 14
  • 73
  • 109
  • please see my comment in reply to Jordan M. I'd appreciate your advice. – VikR Sep 18 '19 at 21:34
  • The migrations are applied in a sequence. If you remove migrations and then need to make changes, you will not be able to apply the new migrations to your production database. Don't delete anything. – Mad Wombat Sep 18 '19 at 21:36
  • @MadWombat I am running into errors in production after deleting my dev database, please recommend what I'm doing wrong https://stackoverflow.com/questions/63111620/does-deleting-migrations-and-database-in-dev-cause-issues-when-pushing-to-produ – Padoga Jul 27 '20 at 10:19
2
  1. The json and js files are unrelated to the django migrations as well as __pycache__ folder. You can delete all off them.
  2. If you mean "previously applied and no longer needed as the project only needs the latest version of the migrations" you don't want to remove but squash them instead with squashmigrations which reduces the files you have to two, init file and the initial migration file, this way your project still works.
  3. If by remove you mean you no longer need them because you already changed the models so much that the previous migrations aren't even used other than being applied and unapplied without ever being used, doesn't matter, go to step 2 and do that instead of deleting the files manually. When you create migrations on your applications one by one, you also create migration dependency tree, well, django does. And it is really hard to keep track of after some point, if you try to delete everything thinking you can create new migration files with ease, trust me as someone who experienced otherwise, it does not work like that. It is way simpler to let django handle the migration squashing, it optimizes the migration meaning that it also deletes the unused ones in your final state.

More to read at: https://docs.djangoproject.com/en/2.2/topics/migrations/#migration-squashing

Işık Kaplan
  • 2,815
  • 2
  • 13
  • 28
0

Having marked one of the answers provided previously as being accepted, here is a summary of a few things I learned:

  • Deleting Django migrations is generally a bad idea.
  • Django keeps track of what's in your db through these migration files, as well as through a table it creates in your db, and if you delete any of this Django will start throwing errors on migrate that can be hard to fix.

I was getting some of those hard-to-fix errors. Here is what I did to fix it:

  • Ran migrate on the production server.
  • When I got an error, it would tell me how the db was out of sync with what Django expected. I corrected that manually by directly editing the db with an sql client.
  • E.g. If it said a key existed that wasn't supposed to exist, I deleted the relevant index from the indicated table.
  • Or if it said a table existed that wasn't supposed to exist, I backed up the table to a file, and deleted the table. Migrate then created the table, and then I repopulated it with data from the backup.
  • In the case of many-to-many tables, once Django had re-created them, I deleted all the new Django-created tables, and restored them from a backup created on my local dev system, which had already had all the latest migrations run on it.

Eventually I was able to complete all migrations successfully.

I have a feeling I lucked out and the above won't work in all cases! I've learned a lot about Django and migrations and will be much more careful about this in the future.

VikR
  • 4,818
  • 8
  • 51
  • 96
0

when you import from third app:

there are 2 step uninstall it

there are use the 'django_celery_beat' app for example.

step1: clean table

python .\manage.py migrate django_celery_beat zero

step2: remove app from INSTALLED_APPS

there are done!!!

this is django document on this.

rogers.wang
  • 416
  • 5
  • 11
0

How to Reset Migrations

if you are using linux/unix os then you can fire this command. delete all migration directory.

find . -path "/migrations/.py" -not -name "init.py" -delete
find . -path "/migrations/.pyc" -delete

Krishna
  • 1
  • 3