Sub topic 11
Cache scope, immutability, size limits, and eviction
This chapter explains the rules that decide which caches a job can read, whether a cache can change after it is saved, and when GitHub deletes caches. It assumes you can write a workflow that uses actions/cache, build a key with hashFiles, and use restore-keys. Those topics are covered in earlier chapters.
Scope: which caches a job can see
Every cache entry is tied to the Git ref that created it. A ref is a branch, a tag, or the merge ref of a pull request, such as refs/pull/42/merge. A job can restore only caches from a limited set of refs:
- The ref the workflow is running on.
- The base branch. For a
pull_requestevent this is the branch the PR targets. - The default branch of the repository.
A job cannot restore caches from other branches. Two feature branches are isolated from each other, and a cache made on a child branch is not visible to the parent.
| Workflow runs on | Can restore caches from | Cannot restore caches from |
|---|---|---|
| `main` | `main` | Any feature branch |
| `feature/login` | `feature/login`, `main` | `feature/search` |
| Pull request into `main` | The PR merge ref, `main` | The PR's source branch cache, other PRs |
| Pull request from a fork | The fork PR's merge ref, `main` | Anything the fork created in the base repository's other refs |
A cache saved by a job is always written to the ref that job ran on. A PR job therefore writes to the PR merge ref. That entry is visible only to later runs of the same PR and disappears from the point of view of every other branch.
The practical result is that the default branch should hold a good cache. Every new branch and PR can fall back to it, so the first run of a new branch is usually warm.
How a lookup proceeds
The action tries to find a match in a fixed order. It starts with the exact key, then each restore-keys prefix in the order you listed them, first in the current ref and then in the base and default branches.
When several entries match one prefix, the action restores the most recently created one. A partial hit still counts as a miss for the cache-hit output, because cache-hit is true only for an exact key match. The post step therefore saves a new entry under the full key.
Fork pull requests are a special case. A workflow triggered by a fork PR runs with a read-only token and cannot write caches into the base repository's branches. Its cache is stored under its own merge ref.
Version: path and compression also matter
The key is not the only thing that identifies a cache. Each entry also has a version, derived from the path values and the compression method. Two entries with the same key but different paths are treated as different caches. The same applies when one was made on a Windows runner and the other on Linux, because Windows runners can use a different compression tool.
This is why ${{ runner.os }} belongs in the key. Without it, a Linux job might find an entry that matches by key but has an incompatible version, and the restore silently misses.
Immutability: a key is written once
A cache entry cannot be changed after it is created. If you save under a key that already exists, the save does not overwrite it. The job log shows a message like this:
Failed to save: Unable to reserve cache with key Linux-npm-abc123, another job may be creating this cache.This also happens when two jobs miss at the same time and both try to save the same key. One wins and the other prints the message. It is not a failure of the job.
Immutability has one important consequence. A key that never changes freezes the cache at its first contents. Consider this key:
key: ${{ runner.os }}-npmThe first run saves Linux-npm. Every later run gets an exact hit and skips saving, so new dependencies never reach the cache, and the install step slowly redownloads more and more. The fix is to include something in the key that changes whenever the contents should change, such as hashFiles('**/package-lock.json'). Then a changed lock file produces a new key and a new entry.
The rule: make the key change when the cached content should change, and use restore-keys to keep the old entry useful as a starting point.
Size limits
GitHub limits the total size of all caches in a repository. The default is 10 GB. Repository administrators on some plans can raise the limit, and extra storage is billed. Check the current limit in your repository settings, because GitHub has changed these options over time.
There is also a rate limit on uploads and downloads per repository. If a matrix of many jobs all save at once, some saves can be throttled. Keep this in mind when you give every matrix job its own cache entry.
Practical ways to stay under the limit:
- Cache the package manager's download cache (
~/.npm) and notnode_modules. Download caches are smaller and work across Node.js versions. - Do not cache build output that is cheap to rebuild.
- Avoid putting the commit SHA in the key. Every commit creates a new entry that no later commit can use.
- Delete caches you no longer need, as shown below.
Eviction: how entries disappear
GitHub removes caches in two situations:
- Age. A cache that has not been accessed for 7 days is deleted. A restore counts as an access, so entries that are used regularly stay.
- Total size. When the repository goes over its limit, GitHub deletes the least recently accessed entries until the total is back under the limit.
The second rule creates a common failure. Suppose many PRs each save a large cache. They fill the 10 GB limit, and GitHub evicts the oldest entries. The cache on main may be among them if no recent run has touched it. The next PR then starts cold. This is called cache thrashing. It looks like random misses, although the cause is lack of space.
Because of this, keep PR caches small, and consider letting only the default branch save caches while PRs only restore them. The split actions described in a later chapter make that possible.
Inspecting and deleting caches
You can see the entries on the Actions tab under Caches. You can also use the GitHub CLI. gh cache list shows each entry's ID, key, size, ref, and last access time.
gh cache list --limit 20
gh cache list --ref refs/heads/main --sort size --order desc
gh cache delete Linux-npm-abc123
gh cache delete --allDeleting needs write access to the repository. In a workflow, the GITHUB_TOKEN needs the actions: write permission.
A useful pattern is to remove a PR's caches when the PR closes. Those entries can never be restored by other branches, so they only take space until they age out.
name: Clean up PR caches
on:
pull_request:
types: [closed]
jobs:
cleanup:
runs-on: ubuntu-latest
permissions:
actions: write
steps:
- name: Delete caches for this PR
run: |
ids=$(gh cache list --ref "$BRANCH" --limit 100 --json id --jq '.[].id')
for id in $ids; do
gh cache delete "$id"
done
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_REPO: ${{ github.repository }}
BRANCH: refs/pull/${{ github.event.pull_request.number }}/mergeCommon errors
- A new branch always starts cold. The default branch has no cache with a matching key or prefix. Run the workflow on
mainso it saves one. - The cache never updates. The key is constant. Add
hashFilesof the lock file. - Misses after many PRs. Total size is over the limit and eviction removed the entries. Check
gh cache listsorted by size. - Restore misses on a different OS. The key lacks
runner.os, or the version differs. Add the OS to the key. - "Unable to reserve cache" in the log. Another job saved the same key first. This is harmless.
Exercise
A workflow uses key: npm-cache with no hashFiles and no restore-keys. A developer adds a new dependency to package-lock.json, but every run still downloads that dependency. Which rule explains this, and how do you fix it?
Show answer
Cache entries are immutable. The first run saved npm-cache, and later runs get an exact hit, so the post step skips saving and the entry never gains the new dependency. Fix it by making the key depend on the lock file, for example key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}, with restore-keys: ${{ runner.os }}-npm-. A changed lock file then produces a new key, restores the old entry as a partial match, and saves an updated entry.
Exercise
A feature branch feature/a saved a cache. Can a job on feature/b restore it? Can a job on main restore it? Name the one kind of ref that both branches can read.
Show answer
Neither can restore it. Caches are visible only to the ref that created them and to refs that fall back to the base or default branch, so sibling branches are isolated and the default branch cannot read a child branch's cache. Both branches can read caches created on the default branch.