Skip to content

Instantly share code, notes, and snippets.

@artem-hatchenko
Last active January 5, 2023 15:19
Show Gist options
  • Save artem-hatchenko/e1fb220d6de8a9ddd61c8cdddf0bc1af to your computer and use it in GitHub Desktop.
Save artem-hatchenko/e1fb220d6de8a9ddd61c8cdddf0bc1af to your computer and use it in GitHub Desktop.
import sys
import boto3
 
def cleanup():
    get_last_modified = lambda obj: int(obj['LastModified'].strftime('%s'))
 
    s3 = boto3.client('s3')
    result = s3.list_objects(Bucket=bucket, Delimiter='/')
    for dir in result.get('CommonPrefixes'):
        print('Directory: ' + str(dir['Prefix']))
        artifacts_listing = s3.list_objects_v2(Bucket = bucket, Prefix = dir.get('Prefix'))['Contents']
        artifacts_sorted = [obj['Key'] for obj in sorted(artifacts_listing, key=get_last_modified)]
        for artifact in artifacts_sorted[:-keep_last]:
            print('Deleting artifact: ' + str(artifact))
            s3.delete_object(Bucket = bucket, Key = artifact)
 
if sys.argv[1:] and sys.argv[2:]:
    bucket = sys.argv[1]
    keep_last = int(sys.argv[2])
    cleanup()
else:
    print("This script for cleanup old artifacts in S3 bucket")
print(f"Usage: python3 {sys.argv[0]} {{BUCKET_NAME}} {{NUMBER_OF_THE_LAST_KEEPING_ARTIFACTS}}")
print(f"Example: python3 {sys.argv[0]} artifacts-artem-services 3")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment