aws / aws-cli

Universal Command Line Interface for Amazon Web Services
Other
15.52k stars 4.12k forks source link

Delete all buckets at once command line #2471

Closed Duna closed 7 years ago

Duna commented 7 years ago

./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}$"

JordonPhillips commented 7 years ago

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?

Duna commented 7 years ago

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

JordonPhillips commented 7 years ago

You would need to implement that as a script

nodeit commented 7 years ago

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://{}

mariquit commented 6 years ago

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}'

Duna commented 6 years ago

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 --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}'

— 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 .

swaminator commented 6 years ago

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

johnmariquit commented 6 years ago

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)

Mahmood787 commented 3 years ago

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

i copy pasted this script and got this error Parameter validation failed: ": Bucket name must match the regex "^[a-zA-Z0-9.-_]{1,255}$" or be an ARN matching the regex "^arn:(aws) is there any solution i have more than 100 bucket.

uday-projectn commented 2 years ago

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://{}

With first command, few buckets got deleted. But few got below errors.


remove_bucket failed: s3://n-tfst-bndr-15b9cc An error occurred (BucketNotEmpty) when calling the DeleteBucket operation: The bucket you tried to delete is not empty. You must delete all versions in the bucket.
remove_bucket failed: s3://n-tfst-bndr-63ae44 An error occurred (BucketNotEmpty) when calling the DeleteBucket operation: The bucket you tried to delete is not empty. You must delete all versions in the bucket.
remove_bucket failed: s3://n-tfst-bndr-b94dec An error occurred (BucketNotEmpty) when calling the DeleteBucket operation: The bucket you tried to delete is not empty. You must delete all versions in the bucket.
remove_bucket failed: s3://n-tfst-defa An error occurred (BucketNotEmpty) when calling the DeleteBucket operation: The bucket you tried to delete is not empty. You must delete all versions in the bucket.
remove_bucket failed: s3://n-tfst-dflta-39818a An error occurred (BucketNotEmpty) when calling the DeleteBucket operation: The bucket you tried to delete is not empty. You must delete all versions in the bucket.
remove_bucket failed: s3://n-tsft-bndr-b94dec An error occurred (BucketNotEmpty) when calling the DeleteBucket operation: The bucket you tried to delete is not empty. You must delete all versions in the bucket.
philippos86 commented 2 years ago

@uday-projectn

With first command, few buckets got deleted. But few got below errors.

I faced the same problem. I had to delete versioned buckets separately. That was a bit easy, thanks to the answer here.

In my case I had only 2 versioned buckets, for experimental purposes, so I didn't invest much time in trying to merge the solution of @nodeit with the answer I shared, but I'm sure it must be doable somehow ..

george-chenvibes commented 2 years ago

@Mahmood787 Did you ever figure out a solution? I'm running into the same issue

Duna commented 2 years ago

Not yet. I nivel Away rrom aws

On Fri, 29 Jul 2022 at 18:21, george-chenvibes @.***> wrote:

@Mahmood787 https://github.com/Mahmood787 Did you ever figure out a solution? I'm running into the same issue

— Reply to this email directly, view it on GitHub https://github.com/aws/aws-cli/issues/2471#issuecomment-1199519261, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA473QRME54LDS2VWSNFJRLVWPZHPANCNFSM4DCC6LSA . You are receiving this because you authored the thread.Message ID: @.***>

-- Sent from Gmail Mobile