Skip to content

Prune orphaned plugin instance files

Jennings Zhang edited this page Mar 15, 2022 · 4 revisions

Files created by plugin instances are not removed from swift when the plugin instance is deleted. This is a script which you run in manage.py shell to prune orphaned plugin instance files.

Warning: this could take a while and use a lot of memory, depending on the number of files in swift.

from plugininstances.models import PluginInstanceFile
from core.swiftmanager import SwiftManager
from django.conf import settings

# username of user who owns the feeds of the plugin instances you want to prune
USER = 'jennings'

swift_manager = SwiftManager(settings.SWIFT_CONTAINER_NAME, settings.SWIFT_CONNECTION_PARAMS)

known_files = frozenset(f.fname.name for f in PluginInstanceFile.objects.all())
swift_files = swift_manager.ls(USER)
orphans = [f for f in swift_files if f.startswith(f'{USER}/feed_') and f not in known_files]

# optionally, save to a file for review
with open('/tmp/orphans.txt', 'w') as of:
    for o in orphans:
        _ = of.write(o)
        _ = of.write('\n')

# optionally, count how much you're going to delete
connection = swift_manager.get_connection()
infos = [  # this takes a while
    connection.head_object(swift_manager.container_name, orphan_name)
    for orphan_name in orphans
]
total_size = sum(info['content-length'] for info in infos)
print(f'Found {total_size / 1e9:.3f}GB of orphaned data.')

# are you sure you want to do this?
for i, orphaned_file in enumerate(orphans):
    swift_manager.delete_obj(orphaned_file)
    print(f'\rDeleting {i} / {len(orphans)}', end='')

print(' done. ')
Clone this wiki locally