def orphan_cleanup(): """ Delete all orphan Content and Artifact records. Go through orphan Content multiple times to remove content from subrepos. This task removes Artifact files from the filesystem as well. """ progress_bar = ProgressReport( message="Clean up orphan Content", total=0, code="clean-up.content", done=0, state="running", ) while True: content = Content.objects.filter( version_memberships__isnull=True).exclude( pulp_type=PublishedMetadata.get_pulp_type()) content_count = content.count() if not content_count: break progress_bar.total += content_count progress_bar.save() # delete the content for c in queryset_iterator(content): progress_bar.increase_by(c.count()) c.delete() progress_bar.state = "completed" progress_bar.save() # delete the artifacts that don't belong to any content artifacts = Artifact.objects.filter(content_memberships__isnull=True) progress_bar = ProgressReport( message="Clean up orphan Artifacts", total=artifacts.count(), code="clean-up.content", done=0, state="running", ) progress_bar.save() counter = 0 interval = 100 for artifact in artifacts.iterator(): # we need to manually call delete() because it cleans up the file on the filesystem artifact.delete() progress_bar.done += 1 counter += 1 if counter >= interval: progress_bar.save() counter = 0 progress_bar.state = "completed" progress_bar.save()
def orphan_cleanup(content_pks=None, orphan_protection_time=settings.ORPHAN_PROTECTION_TIME): """ Delete all orphan Content and Artifact records. Go through orphan Content multiple times to remove content from subrepos. This task removes Artifact files from the filesystem as well. Kwargs: content_pks (list): A list of content pks. If specified, only remove these orphans. """ progress_bar = ProgressReport( message="Clean up orphan Content", total=0, code="clean-up.content", done=0, state="running", ) while True: content = Content.objects.orphaned( orphan_protection_time, content_pks).exclude(pulp_type=PublishedMetadata.get_pulp_type()) content_count = content.count() if not content_count: break progress_bar.total += content_count progress_bar.save() # delete the content for c in queryset_iterator(content): progress_bar.increase_by(c.count()) c.delete() progress_bar.state = "completed" progress_bar.save() # delete the artifacts that don't belong to any content artifacts = Artifact.objects.orphaned(orphan_protection_time) progress_bar = ProgressReport( message="Clean up orphan Artifacts", total=artifacts.count(), code="clean-up.content", done=0, state="running", ) progress_bar.save() counter = 0 interval = 100 for artifact in artifacts.iterator(): # we need to manually call delete() because it cleans up the file on the filesystem artifact.delete() progress_bar.done += 1 counter += 1 if counter >= interval: progress_bar.save() counter = 0 progress_bar.state = "completed" progress_bar.save()