Skip to content

Instantly share code, notes, and snippets.

@illusion0001
Forked from DPatrickBoyd/remove_gh_artifacts.sh
Last active October 8, 2024 09:39
Show Gist options
  • Save illusion0001/4ac8904f2b6a6057fb56ae33befd23b6 to your computer and use it in GitHub Desktop.
Save illusion0001/4ac8904f2b6a6057fb56ae33befd23b6 to your computer and use it in GitHub Desktop.
# Install the GitHub CLI tool by following the instructions in the official documentation: https://cli.github.com/manual/installation
# Make sure you auth first to github with 'gh auth login'
# Powershell version
$REPO_OWNER = "OWNER"
$REPO_NAME = "REPO_NAME"
$PAGE = 1
while ($true) {
# Retrieve a page of artifacts
$ART_EXIST = gh api "repos/$REPO_OWNER/$REPO_NAME/actions/artifacts?per_page=100&page=$PAGE" | ConvertFrom-Json
$ARTIFACTS = $ART_EXIST.artifacts
#Write-Output $ART_EXIST.artifacts
Write-Output $PAGE
#Write-Output $ARTIFACTS
# If there are no more artifacts, exit the loop
if ($ART_EXIST.total_count -eq 0) {
break
}
# Loop through the artifacts on this page and delete the old ones
foreach ($ARTIFACT in $ARTIFACTS) {
$ARTIFACT_NAME = $ARTIFACT.name
$ARTIFACT_ID = $ARTIFACT.id
Write-Output "Deleting artifact $ARTIFACT_NAME (ID: $ARTIFACT_ID)..."
gh api "repos/$REPO_OWNER/$REPO_NAME/actions/artifacts/$ARTIFACT_ID" -X Delete
}
# Increment the page counter
$PAGE++
}
#!/bin/bash
# Change the date under CUTOFF_DATE to change how far back you want to delete
# Install the GitHub CLI tool by following the instructions in the official documentation: https://cli.github.com/manual/installation
# Make sure you auth first to github with 'gh auth login'
REPO_OWNER="OWNER"
REPO_NAME="REPO_NAME"
CUTOFF_DATE=$(date --date='30 days ago' +'%Y-%m-%dT%H:%M:%SZ')
PAGE=1
while true; do
# Retrieve a page of artifacts
ART_EXIST=$(gh api repos/$REPO_OWNER/$REPO_NAME/actions/artifacts?per_page=100\&page=$PAGE | jq -r '.artifacts[]')
ARTIFACTS=$(gh api repos/$REPO_OWNER/$REPO_NAME/actions/artifacts?per_page=100\&page=$PAGE | jq -r '.artifacts[] | select(.created_at < "'"$CUTOFF_DATE"'") | .id')
echo $PAGE
# If there are no more artifacts, exit the loop
if [[ -z "$ART_EXIST" ]]; then
break
fi
# Loop through the artifacts on this page and delete the old ones
for ARTIFACT_ID in $ARTIFACTS; do
ARTIFACT_NAME=$(gh api repos/$REPO_OWNER/$REPO_NAME/actions/artifacts/$ARTIFACT_ID | jq -r '.name')
echo "Deleting artifact $ARTIFACT_NAME (ID: $ARTIFACT_ID)..."
gh api repos/$REPO_OWNER/$REPO_NAME/actions/artifacts/$ARTIFACT_ID -X DELETE
done
# Increment the page counter
PAGE=$((PAGE+1))
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment