Skip to content

Instantly share code, notes, and snippets.

@hardiksondagar
Created July 30, 2024 10:22
Show Gist options
  • Save hardiksondagar/6c8eba695a8aa5c781ae147b8518c6d6 to your computer and use it in GitHub Desktop.
Save hardiksondagar/6c8eba695a8aa5c781ae147b8518c6d6 to your computer and use it in GitHub Desktop.
Delete Github Artifacts
# Set variables
GITHUB_TOKEN="<YOUR_GITHUB_PERSONAL_TOKEN>"
OWNER="<YOUR_ORG_OR_USERNAME>"
REPO="<YOUR_REPO_NAME>"
# Initialize page number and per_page value
page=1
per_page=100 # Maximum allowed per page
# Loop through pages
while true; do
echo "Fetching page $page of artifacts..."
# Fetch artifacts for the current page
response=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/repos/$OWNER/$REPO/actions/artifacts?per_page=$per_page&page=$page")
# Extract artifact IDs
artifact_ids=$(echo "$response" | jq -r '.artifacts[] | .id')
# Check if no artifacts are returned
if [ -z "$artifact_ids" ]; then
echo "No more artifacts found."
break
fi
# Iterate over each artifact ID and delete
for artifact_id in $artifact_ids; do
echo "Deleting artifact ID: $artifact_id"
curl -X DELETE -H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/repos/$OWNER/$REPO/actions/artifacts/$artifact_id"
done
# Move to the next page
page=$((page + 1))
done
echo "All artifacts have been deleted."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment