Remove Dead Tags

I’ve noticed my django-tagging install has been giving a lot of empty entries when doing a lookup on a tag. Tonight I finally got around to looking at what was causing this. This is surely not the best way to do this, but at 12:00am on a weekday, well, I shouldn’t be doing it in the first place… I first wanted to see what type of content was generating the error:

for item in TaggedItem.objects.all():
    try:
        print item.object
    except:
        print item.content_type_id

Now that I could see what was causing it (I had removed an app that used django-tagging, but it left the tags with empty pointers). Removing the empty tags was easy enough:

for item in TaggedItem.objects.all():
    try:
        print item.object
    except:
        item.delete()

No more hanging TaggedItems.