Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Docker Registry S3 cleanup script

Docker Registry S3 cleanup script

This simply requires you to install the boto3 package and set the DOCKER_BUCKET variable to point at the bucket you would like to clean up, and the rest should be handled automatically. That's is presuming you have credentials to manage the S3 bucket in one of the default locations where Boto go to look for them.

Since the script more or less traverses through your entire S3 bucket, it probably makes sense to only run it infrequently, like daily or weekly, depending on the amount of repositories and layers you have and the amount of updates on the registry in total.

#!/usr/bin/env python
import json
import boto3
DOCKER_BUCKET = 'docker-registry'
def main(s3):
bucket = s3.Bucket(DOCKER_BUCKET)
used_images = set()
for key in bucket.objects.filter(Prefix='registry/repositories'):
if (key.key.endswith('/_index_images')):
data = json.loads(key.get()['Body'].read().decode('utf-8'))
for item in data:
used_images.update([item.get('id')])
for key in bucket.objects.filter(Prefix='registry/images'):
image_id = key.key.split('/', 3)[2]
if image_id not in used_images:
print(key.key)
key.delete()
if __name__ == '__main__':
main(boto3.resource('s3'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment