aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

feat(cli): garbage collect s3 assets (under `--unstable` flag)

Open kaizencc opened this issue 1 year ago • 3 comments

S3 Asset Garbage Collection

This PR introduces a new CLI command under the new --unstable flag. This flag ensures that users understand and opt-in to experimental or incomplete CLI features.

cdk gc will garbage collect unused assets in your bootstrapped S3 bucket. It goes through each object in the bucket, checks to see if the asset hash shows up in a cloudformation stack, and if not, tags the object as unused and/or deletes the object (depending on your configuration).

THIS COMMAND WILL DELETE OBJECTS IN YOUR BOOTSTRAPPED S3 BUCKET

basic garbage collection (immediately deletes objects that are unused):

cdk gc aws://0123456789012/us-east-1 \
  --unstable='gc' \
  --type='s3'

garbage collection with a buffer (deletes unused objects > # of days specified):

cdk gc aws://0123456789012/us-east-1 \
  --unstable='gc' \
  --type='s3' \
  --rollback-buffer-days=30

garbage collection with a created at buffer (deletes unused objects only if they have lived longer than this many days):

cdk gc aws://0123456789012/us-east-1 \
  --unstable='gc' \
  --type='s3' \
  --created-at-buffer-days=5

garbage collect a specific bootstrap stack:

cdk gc aws://0123456789012/us-east-1 \
  --unstable='gc' \
  --type='s3' \
  --bootstrap-stack-name=cdktest-0lc2i3vebi7-bootstrap-stack

before actually deleting your assets, you will be prompted one last time:

Found 1 objects to delete based off of the following criteria:
- objects have been isolated for > 0 days
- objects were created > 0 days ago

Delete this batch (yes/no/delete-all)?

To disable this, specify the --skip-delete-prompt option.

Todo in another PR

  • [ ] ECR asset collection

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

kaizencc avatar Oct 01 '24 20:10 kaizencc

:arrow_right: PR build request submitted to test-main-pipeline :arrow_left:

A maintainer must now check the pipeline and add the pr-linter/cli-integ-tested label once the pipeline succeeds.

aws-cdk-automation avatar Oct 08 '24 16:10 aws-cdk-automation

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

mergify[bot] avatar Oct 21 '24 18:10 mergify[bot]

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: 0c851bae1f67b2b613f5d95df5f737b660493bea
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

aws-cdk-automation avatar Oct 21 '24 19:10 aws-cdk-automation

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

mergify[bot] avatar Oct 21 '24 19:10 mergify[bot]

Comments on closed issues and PRs are hard for our team to see. If you need help, please open a new issue that references this one.

github-actions[bot] avatar Oct 21 '24 19:10 github-actions[bot]