Closed Bug 1239756 Opened 8 years ago Closed 8 years ago

Purge taggit_tag table of dead tags

Categories

(developer.mozilla.org Graveyard :: Code Cleanup, defect)

defect
Not set
normal

Tracking

(Not tracked)

RESOLVED FIXED

People

(Reporter: robhudson, Unassigned)

References

Details

As content types have been removed from MDN, we have tags in the taggit_tag table that no longer have content associated with them. It might be wise to clean this up.
How to determine which tags are not associated with any content?

I have run this queryset and found following result


>>> tags_content = TaggedItem.objects.all().values_list("tag__id", flat=True)
>>> Tag.objects.exclude(id__in=tags_content).count()
9204

Seems like, the 9204 is the unused Tag
Tags that have no related content should be removed, but tags to models that are no longer present also need cleaning up.

>>> count_data = TaggedItem.objects.values('content_type_id').annotate(Count('content_type_id'))
>>> for data in count_data:
>>>     ct_id = data['content_type_id']
>>>     tag_count = data['content_type_id__count']
>>>     ct = ContentType.objects.get(id=ct_id)
>>>     is_model = ct.model_class() is not None
>>>     print("%s (%s): %d" % (ct.model, "is model" if is_model else "NOT model", tag_count))

Result:

userprofile (NOT model): 33129
filter (is model): 35
user (is model): 8005

The 33,129 TaggedItems can be cleared, and then the ContentTypes for removed for models that have been dropped from the code.

This is production database cleanup, not a code change.
Commits pushed to master at https://github.com/mozilla/kuma

https://github.com/mozilla/kuma/commit/123131edae378d69d4bcc0c4f276d46bb6ad89bf
[Bug 1239756] Purge taggit_tag table of dead tags

https://github.com/mozilla/kuma/commit/2f315de27ff521d966d30798349c946727241106
Merge pull request #4047 from safwanrahman/taggit

[Bug 1239756] Purge taggit_tag table of dead tags
Manually ran cleanup script in stage and prod for stale content due to removed tables:

https://gist.github.com/jwhitlock/20eb434466bab9b192a23476cf03a8ce

Manually ran cleanup of unused tags in stage and prod.

Migrations should be no-ops when applied.
Migration run in production and staging.
Status: NEW → RESOLVED
Closed: 8 years ago
Resolution: --- → FIXED
Product: developer.mozilla.org → developer.mozilla.org Graveyard
You need to log in before you can comment on or make changes to this bug.