Created
February 20, 2020 14:29
-
-
Save imbgar/73827122cdef7a8e22d4d541f9da106d to your computer and use it in GitHub Desktop.
boto3 empty a bucket
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
| |
# very secure credential storage | |
so_access = '' | |
much_secret = '' | |
bucket = '' | |
# make a client with keys | |
client = boto3.client('s3', aws_access_key_id=so_access, aws_secret_access_key=much_secret) | |
paginator = client.get_paginator('list_objects_v2') | |
pages = paginator.paginate(Bucket=bucket) | |
delete_us = dict(Objects=[]) | |
for item in pages.search('Contents'): | |
delete_us['Objects'].append(dict(Key=item['Key'])) | |
# flush once aws limit reached | |
if len(delete_us['Objects']) >= 1000: | |
client.delete_objects(Bucket=bucket, Delete=delete_us) | |
delete_us = dict(Objects=[]) | |
# flush rest | |
if len(delete_us['Objects']): |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment