LinuxCommandLibrary

gh-cache

Manage GitHub Actions caches

TLDR

List caches for the current repository

$ gh cache [[ls|list]]
copy

List caches for a specific repository
$ gh cache [[ls|list]] [[-R|--repo]] [owner]/[repository]
copy

List caches with a specific cache key prefix
$ gh cache [[ls|list]] [[-k|--key]] [key_prefix]
copy

List caches for a specific branch
$ gh cache [[ls|list]] [[-r|--ref]] refs/heads/[branch_name]
copy

List caches sorted by least recently accessed
$ gh cache [[ls|list]] [[-S|--sort]] last_accessed_at [[-O|--order]] asc
copy

Delete a cache by id
$ gh cache delete [cache_id]
copy

Delete a cache by key
$ gh cache delete [cache_key]
copy

Delete all caches
$ gh cache delete [[-a|--all]]
copy

SYNOPSIS

gh cache list|delete [<repository>] [flags]

PARAMETERS

--all
    Delete all caches (delete only)

--created <{YYYY-MM-DD}|{YYYY-MM-DD}..{YYYY-MM-DD}>
    Filter by cache creation time

--hostname <string>
    Hostname (delete only)

--key <string>
    Filter by cache key

--limit <int>
    Maximum number of caches (default 30)

--machine-readable
    Emit JSON instead of table

--ref <string>
    Filter by ref/branch (delete only)

--repo [<string>]
    Repository (OWNER/REPO)

--scope <string>
    Filter by scope (workflow/step)

--size <{<=N}|{>=N}|{N..M}>
    Filter by size in MiB

--updated <{YYYY-MM-DD}|{YYYY-MM-DD}..{YYYY-MM-DD}>
    Filter by last touched time

DESCRIPTION

The gh cache command from GitHub CLI manages caches used in GitHub Actions workflows for a repository. Caches accelerate workflows by reusing dependencies, build artifacts, and other files between runs.

It offers two subcommands: list to display caches with filters like creation time, size, or key; and delete to remove caches selectively or all at once.

Caches are scoped by repository, branch, workflow, and key. Listing shows details like size, creation/update times, and actions. Deletion supports bulk operations via flags like --all or filters.

Requires GitHub CLI installed and authenticated via gh auth login. Operates on current repo by default or specified OWNER/REPO. Useful for cleaning up unused caches to save storage and costs in large projects.

Supports machine-readable JSON output for scripting.

CAVEATS

Requires GitHub CLI ≥2.4.0 and repository write access. Caches auto-expire after 7 days of inactivity. Use cautiously with --all to avoid data loss.

EXAMPLES

gh cache list --limit 10
gh cache delete --key '::ff::' --repo owner/repo
gh cache list --size '>=100' --machine-readable

AUTHENTICATION

Run gh auth login first. Token needs repo or actions:write scopes.

HISTORY

Added in GitHub CLI 2.4.0 (Oct 2022) to address Actions cache management needs. Enhanced in later versions with more filters and JSON support.

SEE ALSO

gh(1), gh workflow(1)

Copied to clipboard