Skip to content

Instantly share code, notes, and snippets.

@JPBM135
Created December 14, 2025 16:11
Show Gist options
  • Select an option

  • Save JPBM135/fd8014e8ae56431c0a08772923531f1c to your computer and use it in GitHub Desktop.

Select an option

Save JPBM135/fd8014e8ae56431c0a08772923531f1c to your computer and use it in GitHub Desktop.
Cleanup old caches
#!/bin/bash
set -e
# Calculate the cutoff date (3 days ago)
# Note: The date command syntax may vary slightly between Linux and macOS.
# For Linux (GNU date):
# CUTOFF_DATE=$(date -d "3 days ago" +%Y-%m-%dT%H:%M:%SZ)
# For macOS/BSD (requires gdate if not using system date, or a different approach):
CUTOFF_TIMESTAMP=$(date -v -3d +%s)
# List caches as JSON, sort by creation time (newest first by default)
# and use a limit to ensure you get all of them if you have many
gh cache list --json id,createdAt --limit 100 --repo "$GITHUB_REPOSITORY" | jq -r '
.[]
# Remove fractional seconds from createdAt (e.g., "2025-12-14T15:41:00.058211Z" -> "2025-12-14T15:41:00Z")
| .createdAt |= (split(".")[0] + "Z")
| select((now - (.createdAt | fromdateiso8601)) > (3 * 24 * 60 * 60))
| .id
' | while read CACHE_ID; do
echo "Deleting cache ID $CACHE_ID..."
gh cache delete "$CACHE_ID" --repo "$GITHUB_REPOSITORY"
done
echo "Deletion process complete."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment