Skip to content

Instantly share code, notes, and snippets.

@sardbaba
Last active May 3, 2021 13:08
Show Gist options
  • Save sardbaba/a4a52aa4a64e8de1bf38f4cfa0261b44 to your computer and use it in GitHub Desktop.
Save sardbaba/a4a52aa4a64e8de1bf38f4cfa0261b44 to your computer and use it in GitHub Desktop.
AWS Boto3 delete object tags from all objects in a S3 bucket/prefix
import boto3
import time
start_time = time.time()
boto3.setup_default_session(profile_name='YOUR_AWS_PROFILE')
s3 = boto3.client('s3')
s3_paginator = s3.get_paginator('list_objects_v2')
BUCKET = 'YOUR-BUCKET-NAME'
PREFIX = 'A/PREFIX/'
def keys(bucket_name, prefix='/', delimiter='/', start_after=''):
prefix = prefix[1:] if prefix.startswith(delimiter) else prefix
start_after = (start_after or prefix) if prefix.endswith(delimiter) else start_after
for page in s3_paginator.paginate(Bucket=bucket_name, Prefix=prefix, StartAfter=start_after):
for content in page.get('Contents', ()):
yield content['Key']
ks = keys(BUCKET, prefix=PREFIX)
for k in ks:
print("> ", k)
s3.delete_object_tagging(Bucket=BUCKET, Key=k)
"""
get_tags_response = s3.get_object_tagging(Bucket=BUCKET, Key=k)
print(get_tags_response)
"""
print("--- %s seconds ---" % (time.time() - start_time))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment