From 5ea959717e10957f453275a7f893dde30ea71bf9 Mon Sep 17 00:00:00 2001 From: Soham Kalghatgi Date: Tue, 24 Sep 2024 16:07:01 +0200 Subject: [PATCH] Updated the delete bucket script to solve the long path bug --- README.md | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 7b75982f..19081034 100644 --- a/README.md +++ b/README.md @@ -286,12 +286,23 @@ foreach ($db in $databases){ To delete the S3 buckets that contains both versioned and non-versioned objects, the buckets must first be emptied. The following PowerShell script can be used to erase all objects within the buckets and then delete the buckets. ```powershell -$profile = "" +$aws_profile = "" $buckets = terraform output s3_buckets | ConvertFrom-Json -foreach ($bucket in $buckets){ - aws s3api delete-objects --bucket $bucket --profile $profile --delete "$(aws s3api list-object-versions --bucket $bucket --profile $profile --query='{Objects: Versions[].{Key:Key,VersionId:VersionId}}')" - aws s3api delete-objects --bucket $bucket --profile $profile --delete "$(aws s3api list-object-versions --bucket $bucket --profile $profile --query='{Objects: DeleteMarkers[].{Key:Key,VersionId:VersionId}}')" - aws s3 rb s3://$bucket --force --profile $profile +foreach ($bucket in $buckets) { + Write-Output "Deleting bucket: $bucket" + $deleteObjDict = @{} + $deleteObj = New-Object System.Collections.ArrayList + aws s3api list-object-versions --bucket $bucket --profile $aws_profile --query '[Versions[*].{ Key:Key , VersionId:VersionId} , DeleteMarkers[*].{ Key:Key , VersionId:VersionId}]' --output json ` + | ConvertFrom-Json | ForEach-Object { $_ } | ForEach-Object { $deleteObj.add($_) } | Out-Null + $n = [math]::Ceiling($deleteObj.Count / 100) + for ($i = 0; $i -lt $n; $i++) { + $deleteObjDict["Objects"] = $deleteObj[(0 + $i * 100)..(100 * ($i + 1))] + $deleteObjDict["Objects"] = $deleteObjDict["Objects"] | Where-Object { $_ -ne $null } + $deleteStuff = $deleteObjDict | ConvertTo-Json + aws s3api delete-objects --bucket $bucket --profile $aws_profile --delete $deleteStuff | Out-Null + } + aws s3 rb s3://$bucket --force --profile $aws_profile + Write-Output "$bucket bucket deleted" } ```