Skip to content

Commit 8680404

Browse files
authored
Merge pull request #48 from Cosmo-Tech/LAL/fix_s3_bucket_delete
fix bucket delete
2 parents 25f51e9 + 7b3d910 commit 8680404

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

cosmotech/coal/cli/commands/s3_bucket_delete.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,20 +94,15 @@ def s3_bucket_delete(
9494

9595
s3_resource = boto3.resource("s3",
9696
**boto3_parameters)
97-
9897
bucket = s3_resource.Bucket(bucket_name)
9998

100-
remove_prefix = False
10199
if file_prefix:
102100
bucket_files = bucket.objects.filter(Prefix=file_prefix)
103-
if file_prefix.endswith("/"):
104-
remove_prefix = True
105101
else:
106102
bucket_files = bucket.objects.all()
107-
for _file in bucket_files:
108-
if not (path_name := str(_file.key)).endswith("/"):
109-
target_file = path_name
110-
if remove_prefix:
111-
target_file = target_file.removeprefix(file_prefix)
112-
LOGGER.info(f"Deleting {path_name}")
113-
bucket.delete_key(_file.key)
103+
104+
boto_objects = [{'Key': _file.key} for _file in bucket_files if _file.key != file_prefix]
105+
if boto_objects:
106+
LOGGER.info(f'Deleting {boto_objects}')
107+
boto_delete_request = {'Objects': boto_objects}
108+
bucket.delete_objects(Delete=boto_delete_request)

0 commit comments

Comments
 (0)