aws s3 rm: Delete Objects from S3 in CI
Remove one or many objects from an S3 bucket or prefix.
aws s3 rm deletes objects from S3. With --recursive it removes everything under a prefix. In CI it is used to clean up old build artifacts, expired preview environments, or temporary upload paths.
Common flags
--recursive- delete all objects under a prefix--exclude/--include- filter which objects are deleted--dryrun- list what would be deleted without deleting--quiet- suppress per-object output
Example in CI
Tear down a preview environment prefix.
shell
aws s3 rm s3://${PREVIEW_BUCKET}/pr-${PR_NUMBER} --recursiveCommon errors in CI
- An error occurred (AccessDenied) - IAM policy lacks s3:DeleteObject
- Unable to locate credentials - OIDC role not configured in the job
- The specified key does not exist - prefix typo; nothing matches
Key takeaways
- aws s3 rm deletes objects; --recursive clears an entire prefix.
- Always test destructive deletes with --dryrun first in CI.
- AccessDenied means the role is missing s3:DeleteObject.
Related guides
aws s3 cp: Copy Files to S3 in CIaws s3 cp copies files and directories to and from S3. Learn the recursive, exclude, and ACL flags plus a CI…
aws s3 sync: Sync a Directory to S3 in CIaws s3 sync uploads only changed files to S3, making it ideal for static site deploys in CI. Learn the delete…
aws s3 ls: List S3 Buckets and Objects in CIaws s3 ls lists buckets, prefixes, and objects in S3. Learn the recursive, human-readable, and summarize flag…