Skip to content

Cache Cleanup

Cache Cleanup #1

Workflow file for this run

name: Cache Cleanup
on:
schedule:
# Run every Sunday at 2 AM UTC
- cron: '0 2 * * 0'
workflow_dispatch:
inputs:
age_limit:
description: 'Cache age limit in days'
required: false
default: '7'
type: string
permissions:
actions: write
jobs:
cleanup:
runs-on: ubuntu-latest
steps:
- name: Cleanup old caches
run: |
echo "Repository: ${{ github.repository }}"
echo "Default branch: ${{ github.event.repository.default_branch }}"
# Set age limit
AGE_LIMIT="${{ github.event.inputs.age_limit || '7' }}"
echo "Age limit: ${AGE_LIMIT} days"
# Get current cache usage
echo "Current cache usage:"
gh api repos/${{ github.repository }}/actions/caches --paginate | jq -r '.actions_caches[] | "\(.key) - \(.size_in_bytes) bytes - \(.created_at)"' | head -20
# Calculate cutoff date
CUTOFF_DATE=$(date -d "${AGE_LIMIT} days ago" -u +"%Y-%m-%dT%H:%M:%SZ")
echo "Cutoff date: ${CUTOFF_DATE}"
# Get list of caches to delete
CACHES_TO_DELETE=$(gh api repos/${{ github.repository }}/actions/caches --paginate | jq -r --arg cutoff "$CUTOFF_DATE" '.actions_caches[] | select(.created_at < $cutoff) | .id')
# Count caches to delete
CACHE_COUNT=$(echo "$CACHES_TO_DELETE" | wc -l)
if [ -z "$CACHES_TO_DELETE" ] || [ "$CACHE_COUNT" -eq 0 ]; then
echo "No caches found older than ${AGE_LIMIT} days"
else
echo "Found ${CACHE_COUNT} caches to delete"
# Delete old caches
for cache_id in $CACHES_TO_DELETE; do
if [ -n "$cache_id" ]; then
echo "Deleting cache ID: ${cache_id}"
gh api --method DELETE repos/${{ github.repository }}/actions/caches/${cache_id} || echo "Failed to delete cache ${cache_id}"
fi
done
fi
# Get list of all branches
ALL_BRANCHES=$(gh api repos/${{ github.repository }}/branches --paginate | jq -r '.[].name')
# Get list of caches
ALL_CACHE_REFS=$(gh api repos/${{ github.repository }}/actions/caches --paginate | jq -r '.actions_caches[].ref')
# Find caches for deleted branches
echo "Checking for caches from deleted branches..."
for cache_ref in $ALL_CACHE_REFS; do
# Extract branch name from ref (format: refs/heads/branch-name)
BRANCH_NAME=$(echo "$cache_ref" | sed 's|refs/heads/||')
# Skip if it's a pull request ref or tag ref
if [[ "$cache_ref" =~ refs/pull/ ]] || [[ "$cache_ref" =~ refs/tags/ ]]; then
continue
fi
# Check if branch still exists
if ! echo "$ALL_BRANCHES" | grep -q "^${BRANCH_NAME}$"; then
echo "Found caches for deleted branch: ${BRANCH_NAME}"
# Get cache IDs for this branch
BRANCH_CACHE_IDS=$(gh api repos/${{ github.repository }}/actions/caches --paginate | jq -r --arg ref "$cache_ref" '.actions_caches[] | select(.ref == $ref) | .id')
# Delete caches for this branch
for cache_id in $BRANCH_CACHE_IDS; do
if [ -n "$cache_id" ]; then
echo "Deleting cache ID ${cache_id} for deleted branch ${BRANCH_NAME}"
gh api --method DELETE repos/${{ github.repository }}/actions/caches/${cache_id} || echo "Failed to delete cache ${cache_id}"
fi
done
fi
done
echo "Cache cleanup completed"
# Show final cache usage
echo "Final cache usage:"
gh api repos/${{ github.repository }}/actions/caches --paginate | jq -r '.actions_caches[] | "\(.key) - \(.size_in_bytes) bytes - \(.created_at)"' | head -10
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}