Aws-cli: Delete all buckets at once command line

Created on 2 Mar 2017  Â·  8Comments  Â·  Source: aws/aws-cli

./aws s3 rm s3://* --recursive --exclude "photosafeduna"

How can i delete all buckets except single one?

I always get the following error:

Invalid bucket name "*": Bucket name must match the regex "^[a-zA-Z0-9.-_]{1,255}$"

closing-soon feature-request needs-discussion s3

Most helpful comment

I just needed to do this same thing and came up with the following one-liner:

Warning: This will delete all of your buckets/files that you have access to. Make sure you really need to do this before running this script!

aws s3 ls | cut -d" " -f 3 | xargs -I{} aws s3 rb s3://{} --force

If you only want to remove _empty_ buckets remove the --force flag:
aws s3 ls | cut -d" " -f 3 | xargs -I{} aws s3 rb s3://{}

All 8 comments

The command to delete a bucket is aws s3 rb. There is no command currently to delete all buckets, so you would need to do that one at a time. You may be able to write a one liner script to do that using xargs, but I'm not sure that's something we would want to implement as a feature since it's so devastating if fat fingered. Thoughts?

at least there is a command to delete empty folders within a bucket?

You would need to implement that as a script

I just needed to do this same thing and came up with the following one-liner:

Warning: This will delete all of your buckets/files that you have access to. Make sure you really need to do this before running this script!

aws s3 ls | cut -d" " -f 3 | xargs -I{} aws s3 rb s3://{} --force

If you only want to remove _empty_ buckets remove the --force flag:
aws s3 ls | cut -d" " -f 3 | xargs -I{} aws s3 rb s3://{}

Use grep to exclude:
aws s3 ls | grep -v photosafeduna | awk '{printf "aws s3 rb s3://%s --force\n",$3}'

And in reverse, if you want to limit it to certain buckets only:
aws s3 ls | grep photosafeduna | awk '{printf "aws s3 rb s3://%s --force\n",$3}'

great! i was waiting for the answer 1 year ago

On Tue, Nov 14, 2017 at 5:46 AM, John Anthony notifications@github.com
wrote:

Use grep to exclude:
aws s3 ls | grep -v photosafeduna | awk '{printf "aws s3 rb s3://%s
--forcen",$3}'

And in reverse, if you want to limit it to certain buckets only:
aws s3 ls | grep photosafeduna | awk '{printf "aws s3 rb s3://%s
--forcen",$3}'

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/aws/aws-cli/issues/2471#issuecomment-344137931, or mute
the thread
https://github.com/notifications/unsubscribe-auth/ADn9wsVj8u9TmIjpqgLO8a67FcvtGkZSks5s2Q0ugaJpZM4MQvLk
.

When I run the command:
aws s3 ls | cut -d" " -f 3 | xargs -I{} aws s3 rb s3://{}
I get xargs: aws: No such file or directory

That's odd. Your xargs seem to indicate that it cannot find the aws command. Try doing a "which aws" (sans the quotes) to see where your aws is being resolved. I suspect you are using an alias called aws, which could explain why the first "aws s3 ls" works, but xargs could not find the command subsequently because it does not handle alias commands well.

If you do a "which aws" and it yields nothing, and yet the "aws s3" command works for you, try to issue the command "alias". If it outputs something like "alias aws=" then just call with xargs the full path to your aws command (ie. /usr/local/bin/aws <-- really depends on how your aws cli was configured)

Was this page helpful?
0 / 5 - 0 ratings