How can I clean up unused AWS SAM CLI artifacts from my S3 bucket?

September 20, 2025 3 answers
Asked by CloudyNinja88
I've noticed that during every deployment, AWS SAM uploads artifacts to a managed S3 bucket, and now it has ballooned in size. I'm unsure about what I can safely delete using a lifecycle rule because I would need to check each AWS resource to determine if it's referenced, like checking the `CodeUri` for Lambda functions. With thousands of objects in this bucket, I'm looking for a way to clean it up without going through each one manually. Has anyone tackled this issue?

Answers

ScriptMaster42
You might want to consider writing a shell script that utilizes the AWS CLI to help you check which artifacts are safe to delete. It could automate some of the referencing process for you!

DataDude73: But what exactly are you suggesting to check? Are you proposing to do reference counting for AWS SAM?

Reply to this answer
DeployWizard99
Generally, you can probably delete most of the artifacts in your bucket, keeping just the most recent ones as a backup for CloudFormation rollbacks. SAM will re-upload what it needs during your next deployment, so you donโ€™t need to worry too much about losing anything important.

CloudyNinja88: That's kind of my main concernโ€”I'm hoping for a way to delete unreferenced items without needing to manually verify each one. It feels like a tedious process!

Reply to this answer
SavingsSeeker12
I get that it looks cluttered, but are they really costing you that much? From what I see, if the objects are quite small, it might just add up to a few cents each month, which isn't worth the hassle of sorting through them. Just keep in mind that standard-tier objects incur minimal fees unless you access them.

CloudyNinja88: Actually, they are costing me quite a bit alreadyโ€”over 10GB in total! Thatโ€™s why I want to tackle this issue. The aesthetics donโ€™t really bother me; itโ€™s the cost that does!

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions