Skip to content

Instantly share code, notes, and snippets.

@jaketripp
Created April 30, 2021 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaketripp/787f504a29f862e4f7f0b3b3fdca1ce8 to your computer and use it in GitHub Desktop.
Save jaketripp/787f504a29f862e4f7f0b3b3fdca1ce8 to your computer and use it in GitHub Desktop.
A python script to fairly quickly delete a large S3 bucket (though Lifecycle policies may be the only option for extremely large buckets)
import boto3
from concurrent.futures import ThreadPoolExecutor
client1 = boto3.client(
'sts',
aws_session_token='my-current-session-token' # EDIT HERE
)
response = client1.get_caller_identity(
)
sts_assumed_role = client1.assume_role(
RoleArn='arn:aws:iam::123456789101:role/my-role', # EDIT HERE
RoleSessionName='mysession'
)
credentials=sts_assumed_role['Credentials']
print(credentials["SessionToken"])
client=boto3.client(
's3',
aws_access_key_id=credentials['AccessKeyId'],
aws_secret_access_key=credentials['SecretAccessKey'],
aws_session_token=credentials['SessionToken'],
)
bucketname = 'my-bucket-name-without-s3-protocol' # EDIT HERE
# i.e. "my-bucket" not "s3://my-bucket"
def deleteobjects(key, versionid):
client.delete_object(Bucket=bucketname, Key=key, VersionId=versionid)
print('Deleting delete marker {0} for version {1}'.format(key, versionid))
#We will need to gather a list of all versions by using the paginator for the list_object_versions call
paginator = client.get_paginator('list_object_versions')
pages = paginator.paginate(Bucket=bucketname)
for page in pages:
listofobjects = page.get('Versions')
listofdeletemarkers = page.get('DeleteMarkers')
if listofobjects != None:
with ThreadPoolExecutor(max_workers=20) as executor:
for objectL in listofobjects:
executor.submit(deleteobjects, key=objectL['Key'], versionid=objectL['VersionId'])
elif listofdeletemarkers != None:
with ThreadPoolExecutor(max_workers=20) as executor:
for deletemarker in listofdeletemarkers:
executor.submit(deleteobjects, key=deletemarker['Key'], versionid=deletemarker['VersionId'])
print('Deletion of objects complete!')
response = client.delete_bucket(Bucket=bucketname)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment