A GitHub Action to delete GCS (Google Cloud Storage) buckets.
- Delete buckets - Delete GCS buckets
- 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 deleted
- Simple integration - Easy to use in GitHub Actions workflows
Configure GCP credentials before using this action.
Use google-github-actions/auth@v2 with Workload Identity Federation (recommended):
- name: Authenticate to Google Cloud
uses: google-github-actions/auth@v2
with:
workload_identity_provider: projects/123456789/locations/global/workloadIdentityPools/my-pool/providers/my-provider
service_account: my-service-account@my-project.iam.gserviceaccount.comUse the GCS emulator for local testing:
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Start GCS emulator
run: |
docker run -d -p 9023:9023 fsouza/fake-gcs-server -scheme http
- name: Delete bucket in emulator
uses: predictr-io/gcs-delete-bucket@v0
env:
STORAGE_EMULATOR_HOST: http://localhost:9023
with:
bucket-name: test-bucket
force-delete: trueDelete a GCS bucket (fails if bucket is not empty):
- name: Delete GCS bucket
uses: predictr-io/gcs-delete-bucket@v0
with:
bucket-name: my-bucketDelete a bucket and all its contents (objects and versions):
- name: Force delete GCS bucket
uses: predictr-io/gcs-delete-bucket@v0
with:
bucket-name: my-bucket
force-delete: trueUse the output to check if bucket was deleted:
- name: Delete bucket
id: delete-bucket
uses: predictr-io/gcs-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 |
GCS bucket name to delete | Yes | - |
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 (for versioned buckets)
- Deletes in batches - Deletes objects in batches with concurrency control
- Deletes the bucket - After all objects are removed, deletes the bucket itself
- All objects in the bucket
- All object versions (if versioning is enabled)
- 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/gcs-delete-bucket@v0
with:
bucket-name: my-bucketYou must explicitly set force-delete: true to delete non-empty buckets:
# This will delete all objects and the bucket
- uses: predictr-io/gcs-delete-bucket@v0
with:
bucket-name: my-bucket
force-delete: trueThe action provides detailed information about what's being deleted:
- Number of objects 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/gcs-delete-bucket@v0
with:
bucket-name: test-bucket-${{ github.run_id }}
force-delete: trueCreate and delete buckets for temporary storage:
- name: Create temporary bucket
uses: predictr-io/gcs-create-bucket@v0
with:
bucket-name: temp-${{ github.run_id }}
- name: Use bucket
run: |
# Upload and process data
gsutil cp data.txt gs://temp-${{ github.run_id }}/
- name: Delete temporary bucket
if: always()
uses: predictr-io/gcs-delete-bucket@v0
with:
bucket-name: temp-${{ github.run_id }}
force-delete: trueOnly delete if certain conditions are met:
- name: Delete bucket on failure
if: failure()
uses: predictr-io/gcs-delete-bucket@v0
with:
bucket-name: my-bucket
force-delete: trueCreate 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: google-github-actions/auth@v2
with:
workload_identity_provider: ${{ secrets.WIF_PROVIDER }}
service_account: ${{ secrets.SERVICE_ACCOUNT }}
- name: Delete bucket
uses: predictr-io/gcs-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
- GCP credentials are not configured
- Required permissions are missing
- Bucket is not empty and
force-deleteis not enabled - Network errors or GCP service issues occur
For buckets with millions of objects:
- Deletion happens in batches of 1000 objects
- Multiple objects are deleted in parallel (50 at a time)
- 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 deleted.
The service account must have these permissions:
{
"roles": [
"roles/storage.admin"
]
}Or these specific permissions:
storage.buckets.deletestorage.buckets.getstorage.objects.liststorage.objects.delete
- 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 - Label your buckets: Label 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: truewith extreme caution - Test thoroughly in non-production environments first
- Consider using lifecycle policies for automatic cleanup instead
MIT
Contributions are welcome! Please open an issue or submit a pull request.
- gcs-create-bucket - Create GCS buckets
- url-to-gcs - Download URL content directly to GCS
For issues, questions, or contributions, please visit the GitHub repository.