Skip to content

Commit 50f32c8

Browse files
committed
Destroy versions in bucket
1 parent 61fbcc4 commit 50f32c8

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

scripts/infrastructure/destroy/destroy-workspace-utils.sh

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/bin/bash
22

3+
export AWS_PAGER=""
4+
35
function _destroy_lambdas() {
46
local workspace=$1
57
SUBSTRING="--$workspace--"
@@ -208,13 +210,25 @@ function _delete_s3_buckets() {
208210
for bucket in $buckets; do
209211
echo "Deleting objects in S3 bucket: $bucket"
210212
# 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"
217229
done
230+
else
231+
echo "No objects to delete or no versions found."
218232
fi
219233

220234
# Delete all non-current versions

0 commit comments

Comments
 (0)