How to empty S3 bucket using AWS CLI [duplicate] - amazon-web-services

This question already has an answer here:
Delete Full S3 Bucket CLI
(1 answer)
Closed 1 year ago.
I'm trying to empty an S3 bucket using CLI.
I tried aws s3 rm --recursive command which doesn't empty my bucket as it has versioned enabled.
I tried aws s3 rb --force command to forcefully delete the bucket, which doesn't work as well. It throws this error BucketNotEmpty: The bucket you tried to delete is not empty. You must delete all versions in the bucket.
I really need to get this done using CLI. Is there a way to do it. Please help. The end goal is to delete the bucket. Thanks in advance.

If you can only use the CLI try this:
aws s3api delete-objects \
--bucket ${bucket_name} \
--delete "$(aws s3api list-object-versions \
--bucket "${bucket_name}" \
--output=json \
--query='{Objects: Versions[].{Key:Key,VersionId:VersionId}}')"

Related

Why is S3 CLI returning no CommonPrefixes?

I'm trying to list the 'folders' in a S3 bucket under a given prefix.
aws --profile my-profile s3api list-objects-v2 --bucket my-bucket --prefix releases/com/example/app/ --delimiter / --query 'CommonPrefixes[*].Prefix'
There are dozens of folders under the prefix, each containing many files, I I should be able to list the folders., i.e. they do exist
There is no CommonPrefixes returned by this query, so I get null as an output. What am I doing wrong?
running aws-cli/2.0.0 Python/3.7.5 Windows/10 botocore/2.0.0dev4 in Git Bash terminal on Windows.

Delete multiple files through aws cli not work

I have to delete the several .tar files on my s3 bucket. I run the command for doing this through aws cli but getting the error i.e
Error parsing parameter '--delete': Expected: '=', received: ''' for input:
'{Objects:[{Key:2019-03-27T160001Z.tar},{Key:2019-03-27T170001Z.tar}]}'
My aws version is : aws-cli/1.16.136 Python/3.6.0 Windows/10 botocore/1.12.126
My command is:
aws s3api delete-objects --bucket mybucket --delete '{"Objects":[{"Key":"2019-03-27T160001Z.tar"},{"Key":"2019-03-27T170001Z.tar"}]}'
Is there anyone who can guide me on where I am doing mistake. Any help is really appreciated.
You command worked fine for me. As per delete-objects — AWS CLI Command Reference, you can also use:
aws s3api delete-objects --bucket mybucket --delete 'Objects=[{Key=2019-03-27T160001Z.tar},{Key=2019-03-27T170001Z.tar}]'

AWS S3 Bucket counts

I have tried to run these commands:
aws s3api list-objects --bucket bucket --profile key --output json --query "[length(Contents[])]"
and
aws s3 --profile=key ls s3://bucket/ --recursive --summarize
but what I'm curious about is that some of the objects have been marked for deletion. Do these commands count the objects marked for deletion or are they excluded?
commands shared here will list objects/files given in a specified path. these commands will not display any of the deleted objects. for details refer S3 ls

How to delete aws s3 bucket in aws cli

I have the ff s3 bucket, see image below:
I know how to delete them on the page but how can i delete them using aws cli?
I tried using
aws s3api delete-objects --bucket
elasticbeanstalk-ap-southeast-1-613285248276
but it wont work.
You can use 'rb' option
E.g. aws s3 rb s3://elasticbeanstalk-ap-southeast-1-613285248276
For more details click here
If the bucket is not empty then use the --force flag
The following commands deletes the Bucket from S3
aws s3api delete-bucket --bucket "your-bucket-name" --region "your-region".
I am deleting S3 bucket from following command without any extra hustle of emptying it first
aws s3 rb s3://<BUCKATE-NAME> --force
for example
aws s3 rb s3://alok.guha.myversioningbucket --force
PS : Make sure you have programmatic access with policy "AmazonS3FullAccess"

AWS CLI move all files with condition

I must move into another bucket only files changed in the year 2015. How can I write this condition?
aws s3 mv <condition??> s3://bucket1 s3://bucket2 --recursive
I don't think you can directly do that through through the s3 option.
what you can do though is a 2 steps approach:
get the list of files that have been modified after a date
aws s3api list-objects --bucket bucket1" --query 'Contents[?LastModified > `2015-01-01`].[Key]' --output text
Based on this list you can move the items.
I have not tried and not an shell expert but something around this
aws s3api list-objects --bucket "<YOUR_BUCKET>" --query 'Contents[?LastModified > `2015-01-01`].[Key]' --output text | xargs aws s3 mv s3://bucket2/ -