Created
March 6, 2026 03:27
-
-
Save ceshine/674eb34a0741eb33af57cbf3def6b680 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Script to delete artifacts from all accessible repositories | |
| # Adapted from the example script published at https://dev.to/muhammadaqib86/how-to-bulk-delete-github-actions-artifacts-with-a-simple-script-macos-guide-38bh | |
| set -euo pipefail | |
| # 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']]") | |
| # Alternative: comment out the above line and hard code the target repositories in the next line | |
| # REPOS=$(printf 'ceshine/repo_1') | |
| 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 || true | |
| # Delete all artifacts | |
| echo "Deleting artifacts for $REPO" | |
| # Use jq to parse artifacts instead of Python | |
| ARTIFACT_JSON=$( { gh api "/repos/$REPO/actions/artifacts" --paginate 2>/dev/null; } | jq -s 'if length == 0 then {artifacts: []} else reduce .[] as $page ({"artifacts": []}; .artifacts += ($page.artifacts // [])) end' || echo '{"artifacts": []}') | |
| ARTIFACT_COUNT=$(echo "$ARTIFACT_JSON" | jq '.artifacts | length') | |
| echo "Found $ARTIFACT_COUNT artifacts" | |
| if [ "$ARTIFACT_COUNT" -eq 0 ] || [ "$ARTIFACT_COUNT" == "null" ]; then | |
| echo "No artifacts to delete" | |
| continue | |
| fi | |
| # Delete each artifact | |
| echo "$ARTIFACT_JSON" | jq -r '.artifacts[].id' | while read -r artifact_id; do | |
| echo "Deleting artifact $artifact_id" | |
| gh api -X DELETE "/repos/$REPO/actions/artifacts/$artifact_id" || echo "Failed to delete artifact $artifact_id" | |
| sleep 1 | |
| done | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment