|
1 | 1 | #!/bin/bash
|
2 | 2 |
|
| 3 | +export AWS_PAGER="" |
| 4 | + |
3 | 5 | function _destroy_lambdas() {
|
4 | 6 | local workspace=$1
|
5 | 7 | SUBSTRING="--$workspace--"
|
@@ -208,13 +210,25 @@ function _delete_s3_buckets() {
|
208 | 210 | for bucket in $buckets; do
|
209 | 211 | echo "Deleting objects in S3 bucket: $bucket"
|
210 | 212 | # List all objects (including versions) in the bucket
|
211 |
| - objects=$(aws s3api list-object-versions --bucket "$bucket" --output json | jq -r '.Versions[] | {Key: .Key, VersionId: .VersionId}') |
212 |
| - |
213 |
| - # Loop through each object and delete it |
214 |
| - if [ "$objects" != "[]" ]; then |
215 |
| - echo "$objects" | jq -c '.[]' | while read -r object; do |
216 |
| - aws s3api delete-object --bucket "$bucket" --key "$(echo "$object" | jq -r '.Key')" |
| 213 | + # Fetch object versions |
| 214 | + objects=$(aws s3api list-object-versions --bucket "$bucket" --output json) |
| 215 | + num_objects=$(echo "$objects" | jq '.Versions | length') |
| 216 | + echo "Number of objects: $num_objects" |
| 217 | + if [ "$num_objects" -gt 0 ]; then |
| 218 | + echo "Processing $num_objects objects..." |
| 219 | + |
| 220 | + # Loop through each object in the Versions array |
| 221 | + echo "$objects" | jq -r '.Versions[] | select(.Key != null and .Key != "" and .VersionId != null) | @json' | while IFS= read -r object; do |
| 222 | + key=$(echo "$object" | jq -r '.Key') |
| 223 | + versionId=$(echo "$object" | jq -r '.VersionId') |
| 224 | + |
| 225 | + #echo "Object: $object" |
| 226 | + echo "Deleting Key: $key" |
| 227 | + #echo "VersionId: $versionId" |
| 228 | + aws s3api delete-object --bucket "$bucket" --key "$key" --version-id "$versionId" |
217 | 229 | done
|
| 230 | + else |
| 231 | + echo "No objects to delete or no versions found." |
218 | 232 | fi
|
219 | 233 |
|
220 | 234 | # Delete all non-current versions
|
|
0 commit comments