-
Notifications
You must be signed in to change notification settings - Fork 101
Prune orphaned plugin instance files
Jennings Zhang edited this page Jul 28, 2023
·
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.storage import connect_storage
from django.conf import settings
# maybe need this
# https://stackoverflow.com/a/27194927
import django
django.setup()
# username of user who owns the feeds of the plugin instances you want to prune
USER = 'rudolph'
swift_manager = connect_storage(settings)
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(int(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. ')