Skip to content

clean-cache.yml

Delete GitHub Actions caches related to a PR/branch and optionally delete a single image from GHCR when an image reference is provided.

Inputs

InputTypeDescriptionRequiredDefault
PR_NUMBERnumberID number of the pull request associated with the cacheNo-
BRANCH_NAMEstringBranch name associated with the cacheNo-
IMAGEstringImage reference to delete (e.g., ghcr.io/owner/repo/service:pr-123)No-
CLEAN_GH_CACHEbooleanWhether to clean GitHub Actions cacheNotrue
CLEAN_GHCR_IMAGEbooleanWhether to delete the specified image from ghcr.ioNofalse
CLEAN_ORPHANED_GHCR_IMAGEbooleanWhether to delete orphaned SHA-only images from ghcr.ioNofalse
RUNS_ONstringRunner labels as JSON array (e.g., '["ubuntu-24.04"]' or '["self-hosted", "linux"]')No["ubuntu-24.04"]

Permissions

ScopeAccessDescription
packageswriteRequired to delete GHCR container package versions
contentsreadRead repository contents
actionswriteManage Actions caches via cache API

Notes

  • Cache deletion logic picks BRANCH_NAME if provided; otherwise derives a PR ref from PR_NUMBER.
  • Use CLEAN_GH_CACHE, CLEAN_GHCR_IMAGE, and CLEAN_ORPHANED_GHCR_IMAGE to control which cleanup operations run.
  • Image deletion job (cleanup-image) only runs when CLEAN_GHCR_IMAGE: true and IMAGE is supplied.
  • Orphaned image cleanup (cleanup-orphaned-image) deletes SHA-only tagged images (7-character hex digests) when CLEAN_ORPHANED_GHCR_IMAGE: true.
  • Multi-arch manifest cleanup is handled automatically when deleting images.
  • Uses GitHub CLI (gh) for improved API interactions and error handling.
  • IMAGE must include a tag (e.g. ghcr.io/my-org/my-image:pr-123); images are deleted via the GitHub Packages API.

Examples

The examples below show how to clean workflow caches and optionally delete orphaned Docker images, covering both targeted and broad cleanup strategies.

Clean cache and delete specific image

Purges the Actions cache entries associated with PR #123 and deletes the specific tagged image from GHCR. Both operations run because CLEAN_GH_CACHE: true and CLEAN_GHCR_IMAGE: true; omit either flag to skip that operation.

yaml
jobs:
  cleanup:
    uses: this-is-tobi/github-workflows/.github/workflows/clean-cache.yml@v0
    with:
      PR_NUMBER: 123
      IMAGE: ghcr.io/this-is-tobi/tools/debug:pr-123
      CLEAN_GH_CACHE: true
      CLEAN_GHCR_IMAGE: true

Clean orphaned SHA-only images

Removes all GHCR image versions tagged only with a 7-character hex SHA — leftover build artifacts from deleted branches. CLEAN_GH_CACHE: false skips cache deletion so only registry cleanup runs. Provide the image name without a tag; the workflow discovers and deletes all matching SHA-only versions.

yaml
jobs:
  cleanup-orphans:
    uses: this-is-tobi/github-workflows/.github/workflows/clean-cache.yml@v0
    with:
      IMAGE: ghcr.io/this-is-tobi/tools/debug
      CLEAN_GH_CACHE: false
      CLEAN_ORPHANED_GHCR_IMAGE: true