Sub topic 11
Cache scope, immutability, size limits, and eviction
This chapter assumes you can already write an actions/cache step with a key built from hashFiles, and that you know what restore-keys do. Here you learn which caches a run can see, why a saved cache never changes, and how GitHub removes old caches.
Scope: which caches a run can read
Every cache entry belongs to the branch (Git ref) of the run that created it. A run can restore a cache from:
- Its own branch.
- The base branch. For a pull request, this is the branch it targets.
- The default branch, usually
main.
A run cannot read caches from sibling branches or from child branches. A feature branch can therefore reuse what main saved, but two feature branches never share caches with each other.
Pull request runs use a merge ref such as refs/pull/42/merge. Caches created there are visible only to runs of that same pull request.
A practical consequence: if only pull requests create caches, each pull request starts cold. Running the same caching job on push to main seeds a cache that every branch can fall back to.
Immutability
A cache entry cannot be changed after it is created. The key is reserved within its scope. If a later run uses a key that already exists, the post-job save step logs that the cache already exists and skips the upload.
This is why a key like ${{ runner.os }}-npm is a mistake. The first run saves it, and no later run can update it, so the cache goes stale. Put a content hash in the key so that changed dependencies produce a new key and a new entry.
The save happens in a post step, and only when the job succeeds. A failed job saves nothing.
Size limits
Limits to know:
| Limit | Value |
|---|---|
| Total cache per repository | 10 GB by default |
| Key length | 512 characters |
| Retention without access | 7 days |
Repository admins on paid plans can raise the total size limit. A cache that is large or rarely reused costs space that more useful entries need.
Eviction
GitHub removes a cache entry in two cases:
- The entry has not been accessed for 7 days.
- The repository total goes over its limit. GitHub then deletes the least recently used entries until the total fits.
Eviction can cause surprising misses. A matrix with many combinations, each saving a large cache under several branches, can fill 10 GB quickly. The entries main needs may then be evicted by pull request caches. Keep entries small, cache only the package manager's download cache instead of node_modules, and avoid saving many near-identical entries.
Inspecting and deleting caches
The GitHub CLI can list and delete entries:
gh cache list --limit 20
gh cache delete Linux-npm-3f2a9c
gh cache delete --allDeleting an entry is the only way to replace a cache under an existing key. Usually it is better to change the key.
Common errors
- Cache never updates. The key does not change when dependencies change. Add
hashFilesto the key. - Pull requests always miss. No cache exists on
mainor the base branch. Run the caching job onpushto the default branch. - Good caches vanish. Total size is near the limit, so least recently used entries are being evicted. Check with
gh cache list. - Nothing saved after a failed run. The save step only runs when the job succeeds. Use the separate
actions/cache/saveaction withif: always()if you need to save anyway.
Exercise
A pull request from branch feature-a misses the cache every time, although feature-b has a cache with the right key. The repository has no cache on main. Why does feature-a miss, and what is the simplest fix?
Show answer
Caches are scoped to branches, and feature-a cannot read caches from a sibling branch such as feature-b. It can only read its own, its base branch's, and the default branch's caches. The simplest fix is to run the caching job on push to main, so main holds a cache that all branches and pull requests can restore.