Skip to content

Instantly share code, notes, and snippets.

@satishsurath
Last active July 25, 2025 21:27
Show Gist options
  • Select an option

  • Save satishsurath/53119a7ce1a219aafbde6d434814c50b to your computer and use it in GitHub Desktop.

Select an option

Save satishsurath/53119a7ce1a219aafbde6d434814c50b to your computer and use it in GitHub Desktop.

How to Bulk Delete GitHub Actions Artifacts with a Simple Script (MacOS Guide)

https://dev.to/muhammadaqib86/how-to-bulk-delete-github-actions-artifacts-with-a-simple-script-macos-guide-38bh

#github#devops#automation#githubactions

If you're using GitHub Actions, you know how quickly artifacts can accumulate and consume your storage quota. In this post, I'll show you how to clean up all your GitHub Actions artifacts across all repositories with a simple bash script - no manual deletion required!

Why Delete GitHub Actions Artifacts?

GitHub Actions artifacts are great for storing build outputs and test results, but they come with some limitations:

  • Storage limits: Free accounts get only 500MB of artifact storage
  • Retention period: Artifacts expire after 90 days by default
  • Cost concerns: Excess storage can lead to charges for pro accounts

Regular cleanup helps you:
✅ Stay under storage limits

✅ Improve repository performance

✅ Maintain better organization 

The Automated Solution

Instead of manually deleting artifacts through the UI, we'll use GitHub's CLI (gh) with a Python helper to:

  1. Find all repositories you have access to
  2. List all artifacts
  3. Delete them in bulk

Prerequisites

Before running the script, ensure you have:

  1. GitHub CLI installed and authenticated:
brew install gh
gh auth login

(Make sure repo scope is enabled - check with gh auth status)

  1. Python 3.13.2 (or compatible version):
python3 --version
# Install if needed:
brew install python@3.13
  1. zsh shell (default on modern MacOS)

The Script Setup

Create a new file called delete-all-artifacts.sh:

#!/bin/bash
# Script to delete artifacts from all accessible repositories
USER="YOUR_USERNAME"  # Replace with your GitHub username

# Fetch all repos with write/admin access
REPOS=$(gh api /user/repos\?affiliation=owner,collaborator,organization_member --paginate | python3 -c "import sys, json; repos = json.load(sys.stdin); [print(repo['full_name']) for repo in repos if repo['permissions']['admin'] or repo['permissions']['push']]")

for REPO in $REPOS; do
  echo "Processing repository: $REPO"

  # Optional: List recent workflow runs
  echo "Listing recent workflow runs for $REPO"
  gh run list --repo "$REPO" --limit 5 --json databaseId,status,conclusion

  # Delete all artifacts
  echo "Deleting artifacts for $REPO"
  ARTIFACT_IDS=$(gh api /repos/"$REPO"/actions/artifacts --paginate | python3 -c "import sys, json; artifacts = json.load(sys.stdin)['artifacts']; print('Found ' + str(len(artifacts)) + ' artifacts'); [print(artifact['id']) for artifact in artifacts]")

  if [ -z "$ARTIFACT_IDS" ]; then
    echo "No artifacts to delete"
  else
    echo "$ARTIFACT_IDS" | tail -n +2 | xargs -n1 -I % bash -c "gh api -X DELETE /repos/\"$REPO\"/actions/artifacts/% && sleep 1"
  fi
done

Make it executable:

chmod +x delete-all-artifacts.sh

How It Works

  1. Repository Discovery

    • Finds all repos where you have push/admin rights
    • Includes personal, collaborator, and organization repos
  2. Artifact Deletion

    • Lists artifacts for each repository
    • Deletes them one by one with a 1-second delay (avoids rate limiting)
  3. Progress Reporting

    • Shows which repository is being processed
    • Reports number of artifacts found and deleted

Running the Script

./delete-all-artifacts.sh

Expected Output

You'll see output like this for each repository:

Processing repository: username/repo-name
Listing recent workflow runs for username/repo-name
[]
Deleting artifacts for username/repo-name
Found 2 artifacts
123
124

Troubleshooting Tips

If you encounter issues:

  1. Python Syntax Errors: Verify the script contents match exactly what's shown above
  2. Permission Problems: Run gh auth refresh -h github.com -s repo
  3. Rate Limiting: Increase the sleep 1 to sleep 2 if needed
  4. Test First: Try on a single repo before bulk deletion:
   gh api /repos/username/repo-name/actions/artifacts

Important Notes

⚠️ This cannot be undone - deleted artifacts are permanently removed

⏳ Takes time for repositories with many artifacts

🔒 Only affects repos where you have write/admin permissions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment