Skip to content

Instantly share code, notes, and snippets.

@vgribok
Last active June 21, 2023 17:14
Show Gist options
  • Save vgribok/62c448f3c66b723b1c42929eed03d389 to your computer and use it in GitHub Desktop.
Save vgribok/62c448f3c66b723b1c42929eed03d389 to your computer and use it in GitHub Desktop.
Dele
# These commands are DANGEROUS! They may DELETE ALL YOUR DATA in AWS S3!
# Use at your own risk! NO WARRANTY OR LIABILITY IS ASSUMED OR WILL BE ACCEPTED!
# The main purpose is to delete multiple (non-empty) S3 buckets using
# bucket name prefix, converted by the script to a wildcard.
# Uses PowerShell Tools for AWS.
# You may need to swtich to bucket's AWS Region if you are getting the following error:
# "The bucket you are attempting to access must be addressed using the specified endpoint.
# Please send all future requests to this endpoint."
Write-Host "Adding all AWS PowerShell commands. It may take a few seconds.."
Import-Module awspowershell.netcore
$bucketNamePrefix = 'bucket-name-prefix'
# Output buckets that will be deleted
Get-S3Bucket | where { $_.BucketName -like "$bucketNamePrefix*" }
# Delete the buckets. Remove -Force flag to confirm every bucket deletion
Get-S3Bucket | where { $_.BucketName -like "$bucketNamePrefix*" } `
| ForEach-Object { Remove-S3Bucket -BucketName $_.BucketName -DeleteBucketContent -Force }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment