A GitHub Action to delete AWS S3 buckets.
- Delete buckets - Delete S3 buckets in any AWS region
- Force delete - Optionally delete all objects and versions before deleting bucket
- Safe by default - Fails if bucket is not empty (unless force-delete is enabled)
- Comprehensive logging - Detailed information about objects and versions deleted
- Simple integration - Easy to use in GitHub Actions workflows
Configure AWS credentials before using this action.
Use aws-actions/configure-aws-credentials@v4 for real AWS environments:
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::123456789012:role/my-github-actions-role
aws-region: us-east-1Use LocalStack as a service container for local testing:
jobs:
test:
runs-on: ubuntu-latest
services:
localstack:
image: localstack/localstack
ports:
- 4566:4566
env:
SERVICES: s3
steps:
- name: Delete bucket in LocalStack
uses: predictr-io/aws-s3-delete-bucket@v0
env:
AWS_ENDPOINT_URL: http://localhost:4566
AWS_ACCESS_KEY_ID: test
AWS_SECRET_ACCESS_KEY: test
AWS_DEFAULT_REGION: us-east-1
with:
bucket-name: 'test-bucket'
force-delete: 'true'Delete an S3 bucket (fails if bucket is not empty):
- name: Delete S3 bucket
uses: predictr-io/aws-s3-delete-bucket@v0
with:
bucket-name: 'my-bucket'Delete a bucket and all its contents (objects and versions):
- name: Force delete S3 bucket
uses: predictr-io/aws-s3-delete-bucket@v0
with:
bucket-name: 'my-bucket'
force-delete: 'true'Delete a bucket in a specific AWS region:
- name: Delete S3 bucket
uses: predictr-io/aws-s3-delete-bucket@v0
with:
bucket-name: 'my-bucket-eu'
region: 'eu-west-1'Use the output to check if bucket was deleted:
- name: Delete bucket
id: delete-bucket
uses: predictr-io/aws-s3-delete-bucket@v0
with:
bucket-name: 'my-bucket'
force-delete: 'true'
- name: Check deletion status
run: |
echo "Bucket deleted: ${{ steps.delete-bucket.outputs.deleted }}"| Input | Description | Required | Default |
|---|---|---|---|
bucket-name |
S3 bucket name to delete | Yes | - |
region |
AWS region for the bucket | No | us-east-1 |
force-delete |
If "true", delete all objects and versions before deleting bucket | No | false |
| Output | Description |
|---|---|
deleted |
Whether the bucket was successfully deleted ("true" or "false") |
When force-delete: 'true' is set:
- Lists all objects - Retrieves all objects in the bucket (up to 1000 at a time)
- Lists all versions - Retrieves all object versions and delete markers (for versioned buckets)
- Deletes in batches - Deletes objects in batches of 1000
- Deletes the bucket - After all objects are removed, deletes the bucket itself
- All objects in the bucket
- All object versions (if versioning is enabled)
- All delete markers
- The bucket itself
By default, the action will fail if the bucket is not empty. This prevents accidental data loss.
# This will FAIL if bucket has any objects
- uses: predictr-io/aws-s3-delete-bucket@v0
with:
bucket-name: 'my-bucket'You must explicitly set force-delete: 'true' to delete non-empty buckets:
# This will delete all objects and the bucket
- uses: predictr-io/aws-s3-delete-bucket@v0
with:
bucket-name: 'my-bucket'
force-delete: 'true'The action provides detailed information about what's being deleted:
- Number of objects deleted
- Number of versions deleted
- Any errors encountered during deletion
Delete test buckets after CI/CD runs:
- name: Run tests
run: npm test
- name: Cleanup test bucket
if: always()
uses: predictr-io/aws-s3-delete-bucket@v0
with:
bucket-name: 'test-bucket-${{ github.run_id }}'
force-delete: 'true'Create and delete buckets for temporary storage:
- name: Create temporary bucket
uses: predictr-io/aws-s3-create-bucket@v0
with:
bucket-name: 'temp-${{ github.run_id }}'
- name: Use bucket
run: |
# Upload and process data
aws s3 cp data.txt s3://temp-${{ github.run_id }}/
- name: Delete temporary bucket
if: always()
uses: predictr-io/aws-s3-delete-bucket@v0
with:
bucket-name: 'temp-${{ github.run_id }}'
force-delete: 'true'Only delete if certain conditions are met:
- name: Delete bucket on failure
if: failure()
uses: predictr-io/aws-s3-delete-bucket@v0
with:
bucket-name: 'my-bucket'
force-delete: 'true'Create a manual workflow to clean up old buckets:
name: Cleanup Old Buckets
on:
workflow_dispatch:
inputs:
bucket-name:
description: 'Bucket name to delete'
required: true
force:
description: 'Force delete (true/false)'
required: true
default: 'false'
jobs:
cleanup:
runs-on: ubuntu-latest
steps:
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE }}
aws-region: us-east-1
- name: Delete bucket
uses: predictr-io/aws-s3-delete-bucket@v0
with:
bucket-name: ${{ github.event.inputs.bucket-name }}
force-delete: ${{ github.event.inputs.force }}The action will fail if:
- Bucket name is empty or invalid
- AWS credentials are not configured
- Required permissions are missing
- Bucket is not empty and
force-deleteis not enabled - Network errors or AWS service issues occur
For buckets with millions of objects:
- Deletion happens in batches of 1000 objects
- Progress is logged for each batch
- GitHub Actions has a maximum run time of 6 hours
Buckets with versioning enabled may take longer to delete because:
- All versions of each object must be listed and deleted
- Delete markers must also be removed
The IAM role or user must have these permissions:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:DeleteBucket",
"s3:ListBucket",
"s3:ListBucketVersions",
"s3:DeleteObject",
"s3:DeleteObjectVersion"
],
"Resource": [
"arn:aws:s3:::*",
"arn:aws:s3:::*/*"
]
}
]
}Note: For production use, restrict the Resource to specific bucket ARNs.
- Use in test environments: Primarily use force-delete in test/CI environments
- Verify bucket name: Double-check bucket names before deleting
- Use conditionals: Use
if: always()orif: failure()for cleanup steps - Tag your buckets: Tag buckets with environment info to prevent accidental deletion
- Backup important data: Always backup important data before deletion
- Use IAM restrictions: Restrict deletion permissions to specific buckets in production
- Deleted objects cannot be recovered (unless versioning/backup is enabled)
- Use
force-delete: 'true'with extreme caution - Test thoroughly in non-production environments first
- Consider using S3 lifecycle policies for automatic cleanup instead
MIT
Contributions are welcome! Please open an issue or submit a pull request.
- aws-s3-create-bucket - Create S3 buckets
- url-to-s3 - Download URL content directly to S3
For issues, questions, or contributions, please visit the GitHub repository.