I've been deploying my AWS SAM application multiple times, and the artifacts are piling up in the managed S3 bucket. It's become massive, and I'm not sure which files I can safely delete. I'd like to set a lifecycle rule but I don't want to risk deleting anything that might still be in use, like resources linked to Lambda functions through `CodeUri`. Has anyone figured out an effective way to manage or automate this cleanup?
3 Answers
In most cases, you can delete everything except maybe the latest few artifacts. AWS SAM will automatically re-upload what’s needed on the next deploy. Just keep a recent batch in case of rollbacks.
I get that it looks messy, but are you really spending that much on storage? If the objects are small, it might just be a few cents a month—probably not worth the hassle of sorting through them all.
Actually, they are costing me. It’s already hit over 10GB, which is why I’m trying to clean this up.
You could consider writing a shell script that uses the AWS CLI to check for unused artifacts. It’s not perfect, but it might streamline the process for you!
What do you mean by checking? Are you suggesting we should do reference counting for AWS SAM?

That's the main concern for me—I'm looking for a way to remove things not referenced anywhere without having to check them all manually.