11

I'd like to get rid of the follow message I get when running migrations:

The following content types are stale and need to be deleted:

    appname | modelname

Any objects related to these content types by a foreign key will also
be deleted. Are you sure you want to delete these content types?

It seems cleaner to encode the decision to delete or not delete in the migrations.

What's the cleanest way to detect and delete stale ContentTypes as part of my migrations? Why is it not done automatically if ContentTypes are created automatically?

jamjar
  • 625
  • 5
  • 13
  • 1
    If content types are used in [generic relations](https://docs.djangoproject.com/en/1.9/ref/contrib/contenttypes/#generic-relations), removing them will trigger the `on_delete` action of the foreign key in those models and potentially cause data loss -- something that any automated action should avoid at all costs. Creating content types cannot cause data loss, so it is safe to do automatically. – knbk Aug 31 '16 at 18:03
  • Is it not possible to detect if content types are used in generic relations? – jamjar Aug 31 '16 at 18:23
  • Possibly related: http://stackoverflow.com/questions/18869414/can-stale-content-types-be-automatically-deleted-in-django – Sebastian Wagner May 09 '17 at 22:34

2 Answers2

26

Try this out to remove safely

python manage.py remove_stale_contenttypes

This command is available > Django 1.11

David Lam
  • 4,689
  • 3
  • 23
  • 34
HamzaMushtaq
  • 1,660
  • 13
  • 13
0

When migrating your database, you can add --noinput to manage.py like so:

manage.py migrate --noinput

This way, users won't be asked.

Sebastian Wagner
  • 2,308
  • 2
  • 25
  • 32
  • Are you sure this works? I have never been asked while migrating... – Ron Nov 27 '20 at 15:04
  • 1
    I know :) But I've been working with django since about 9 years. And I've never seen that this takes care of the content types... – Ron Nov 27 '20 at 15:33