Why GitHub Actions Won't Update a Cache on an Exact Key Hit
In this article
GitHub Actions does not update a cache when the exact cache key is hit because caches are immutable by design—once a cache entry is written, it cannot be modified. This means if your workflow restores a cache using the same key it previously saved, the cached content stays exactly as it was, even if the source files have changed. The only way to get a fresh cache is to use a new key (or a different version of the key) that does not match any existing entry.
Background: How GitHub Actions Caching Works
The actions/cache action lets you save and restore files (like dependency folders or build outputs) across workflow runs. When you run a job, you typically call actions/cache with a key and optionally a restore-keys list. The action first tries to restore a cache that exactly matches the key. If a match is found, it restores that cache and skips the save step (unless you use the lookup-only option). If no exact match is found, it looks for the closest match among restore-keys (a prefix match) and restores that, then after the job finishes, it saves a new cache entry under the full key.
This design is intentional: caches are treated as immutable snapshots. Once a cache entry is created, it cannot be overwritten or edited. This prevents race conditions and corruption that could occur if two jobs tried to modify the same cache simultaneously. The immutability also makes caching predictable—you can always trust that a cache with a given key contains exactly the content that was saved with that key.
Why Immutability Prevents Updates on Exact Key Hit
When your workflow runs and the key you supply exactly matches an existing cache entry, the action restores that entry and does not create a new one. The actions/cache action has a built-in check: if the exact key is found during restore, it marks the cache as a “hit” and skips the save operation. This is documented in the action’s README: “If the exact key is found, the cache is restored and the action will not save a new cache.”
The reason is simple: if the key is the same, the content is assumed to be the same. If you change your dependencies or source files, you should change the key to reflect that. If you don’t, you’re telling the system “this is the same state as before,” so it keeps the old cache. This is not a bug—it’s a deliberate design choice to avoid unnecessary writes and to keep the cache store clean.
How to Choose a Cache Key That Forces Updates
The most common way to ensure your cache updates when your files change is to make the key depend on the content of the files you’re caching. For example, you can hash a lock file:
- For npm: use npm cache with key
- For pip: use pip cache with key
- For Gradle: use
Using hashFiles ensures that any change to the lock file produces a new key, which will not match the old cache, so a new cache is saved after the job runs. You can also include the OS and the dependency manager version in the key to avoid collisions between different environments.
If you want to update the cache even when the key matches (for example, to refresh a cache that stores build artifacts that change every run), you can append a timestamp or a run ID to the key. However, this means you’ll never get a cache hit on subsequent runs, so you’ll always save a new cache—which may be wasteful if the cache is large.
Common Problems and Misconceptions
Misconception: “I changed my files, why is the cache still old?” This happens when your key does not incorporate the file content. If your key is static (e.g., my-cache), it will always match the existing cache, and you’ll never get a fresh one. The fix is to make the key dynamic.
Problem: “My cache is not being saved at all.” If your job fails before the save step, the cache won’t be saved. Also, if you use restore-keys and the exact key is not found, a new cache is saved—but if the exact key is found, it skips saving. So check your logs: if you see “Cache hit” and then “Post job: skipping cache save”, that’s expected.
Problem: “I want to force an update without changing my key.” You can delete the cache from the GitHub UI (Actions → Caches) or use the gh CLI to delete it. Then the next run will not find a match and will save a new cache. Alternatively, you can change the key manually once to invalidate the old cache.
Comparing with Other CI/CD Caching
Other CI systems like Travis CI, CircleCI, or GitLab CI also use immutable caches, but they often handle key generation differently. For example, CircleCI’s save_cache requires you to specify a key and will overwrite an existing cache with the same key (it is mutable). GitLab CI also allows overwriting caches with the same key. GitHub Actions is stricter: it never overwrites. This means you must be more deliberate about key design.
This immutability is actually a safety feature: it prevents a corrupted or partial cache from being written over a good one. But it also means you can’t simply “refresh” a cache by running the same job again. You have to change the key or delete the cache.
Costs and Storage Limits
GitHub Actions has a cache storage limit per repository: 10 GB for free and paid plans (as of 2024). There is no per-cache size limit, but total storage is capped. When you exceed the limit, the oldest caches are evicted automatically. Caches are also evicted if they haven’t been accessed in 7 days. So even if you never update a cache, it will eventually disappear if unused.
Because caches are immutable, every time you save a new cache with a new key, it adds to your storage. If you generate a new key on every run (e.g., with a timestamp), you’ll quickly fill your storage. To avoid this, use restore-keys to fall back to older caches and only save a new one when the key changes meaningfully.
Safety and Best Practices
When designing your caching strategy, follow these guidelines:
- Always include a content hash in your key for dependencies that are locked (e.g.,
package-lock.json). - For caches that are rebuilt every run (like build artifacts), consider using a separate cache with a key that includes a timestamp, but be aware of storage limits.
- Use
restore-keysto fall back to a previous cache when the exact key is not found, which speeds up runs when a dependency hasn’t changed. - Do not cache files that are sensitive or contain secrets—caches are accessible to anyone with read access to the repository.
- Monitor your cache usage in the repository settings to avoid hitting the 10 GB limit.
Frequently Asked Questions
Why does my cache never update even though I changed my code?
Because your cache key is likely static. If the key doesn’t change, the exact key matches, and the cache is restored without saving a new one. Update your key to include a hash of the files you’re caching, like hashFiles('**/package-lock.json').
Can I force GitHub Actions to overwrite a cache with the same key?
No, not directly. The cache is immutable. You can delete the cache manually from the GitHub UI or using the API, then the next run will save a new cache under the same key. Alternatively, change the key to a new value.
What is the difference between key and restore-keys?
The key is the primary identifier. If it matches an existing cache, that cache is restored and no new cache is saved. restore-keys are fallback patterns: if the exact key is not found, the action looks for the most recent cache whose key starts with one of the restore-keys. The restore-keys are never used for saving, only restoring.
Why does my cache save fail with “Cache already exists”?
This error occurs when you try to save a cache with a key that already exists. Because caches are immutable, you cannot overwrite. The action usually skips saving if the key was hit during restore, but if you call actions/cache/save separately with a key that already exists, it will fail. Use a new key or delete the old cache.
How do I delete a cache?
Go to your repository on GitHub, click “Actions” → “Caches” in the sidebar. You’ll see a list of caches with a delete icon. You can also use the GitHub CLI: gh cache delete <cache-id>. After deletion, the next run will not find a match and will save a new cache.
Does using a timestamp in the key always update the cache?
Yes, because a timestamp makes the key unique every run, so the exact key never matches an existing cache. However, this means you’ll never get a cache hit, so you’ll always save a new cache, which can quickly consume your storage quota. Use timestamps only for caches that are small or that you need to refresh frequently.