Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions cosmotech/coal/cli/commands/s3_bucket_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,15 @@ def s3_bucket_delete(

s3_resource = boto3.resource("s3",
**boto3_parameters)

bucket = s3_resource.Bucket(bucket_name)

remove_prefix = False
if file_prefix:
bucket_files = bucket.objects.filter(Prefix=file_prefix)
if file_prefix.endswith("/"):
remove_prefix = True
else:
bucket_files = bucket.objects.all()
for _file in bucket_files:
if not (path_name := str(_file.key)).endswith("/"):
target_file = path_name
if remove_prefix:
target_file = target_file.removeprefix(file_prefix)
LOGGER.info(f"Deleting {path_name}")
bucket.delete_key(_file.key)

boto_objects = [{'Key': _file.key} for _file in bucket_files if _file.key != file_prefix]
if boto_objects:
LOGGER.info(f'Deleting {boto_objects}')
boto_delete_request = {'Objects': boto_objects}
bucket.delete_objects(Delete=boto_delete_request)
Loading