Despite deleting a large amount of files on one of my Amazon S3 buckets, I noticed the bucket size was still sitting high at 230gb.
I checked the size over a period of time, because I had read that sometimes the metrics is delayed 24-48 hours, but even after several days, the size was not dropping.
Upon digging into it, I discovered I had versioning enabled for this bucket so even though I had deleted many files, older versions of those files were still hanging around.
As evidence, I ran the following aws s3api list-objects-v2 command to list objects currently in the bucket:
aws s3api list-objects-v2 \
--bucket YOUR_BUCKET \
--output json \
--query "Contents[].Key" | jq length
This yielded 1683.
I then ran the following command to list noncurrent versions of objects currently in the bucket:
aws s3api list-object-versions \
--bucket YOUR_BUCKET \
--query "length(Versions[?IsLatest==\`false\`])"
This yielded a much higher number: 155545.
So there was my problem: Even though I had cleaaned up the bucket removing many files, S3’s versioning system still had noncurrent versions of those files hanging around.
To address this I had to complete the following steps:
Do this under Properties > Bucket Versioning.
Note that this only applies to new objects added to this bucket, so the next step is necessary to delete any existing noncurrent versions.
Under Management, create a new Lifecycle rule with the following details:
The lifecycle rules run once every 24 hours, and the first time this rule runs it will mark concurrent items as expired, then the second time it runs it will delete those expired items. Because of this, you’ll need to wait 48-36 hours for everything to process.
Here’s a quick-start guide to getting started with aws s3api:
Follow the instructions here to install the AWS CLI...
Verify:
aws --version
The simplest setup:
aws configure
It will ask for:
This writes to:
~/.aws/credentials~/.aws/configIf you use SSO:
aws configure sso
aws sso login
Confirm you’re authenticated:
aws sts get-caller-identity
List buckets:
aws s3api list-buckets
List objects in a bucket:
aws s3api list-objects-v2 --bucket YOUR_BUCKET --max-keys 10
Check a specific object’s metadata:
aws s3api head-object --bucket YOUR_BUCKET --key path/to/file.csv
No subscriptions, no auto-renewals.
Just a simple one-time payment that helps support my free, to-the-point videos without sponsered ads.
Unlocking gets you access to the notes for this video plus all 200+ guides on this site.
Your support is appreciated. Thank you!