Sub topic 14
Retention, size limits, and cleanup
This chapter assumes you can already export and import cache with the registry, gha, and s3 backends, and that you know the difference between mode=min and mode=max. Remote cache grows until something removes it. Each backend handles that differently, so you need to know which one cleans up for you and which one doesn't.
How each backend ages out cache
| Backend | Limit | Automatic removal | Your job |
|---|---|---|---|
| `gha` | About 10 GB per repository by default | Entries unused for 7 days are evicted. When the limit is hit, the least recently used entries go first. | Keep the cache small and scopes few. |
| `registry` | Registry quota, if any | None | Run garbage collection or a lifecycle policy. |
| `s3` | None | None | Add a bucket lifecycle rule. |
GitHub Actions: staying under the quota
The gha backend cleans itself, but the 10 GB budget is shared by every scope in the repository. With mode=max, one large image can fill it. Pull request branches then evict the cache that main depends on, and the next build on main starts cold.
To reduce the pressure:
- Use
mode=minfor images where the final layers are the expensive ones. - Use a small number of scopes. One scope per image is usually enough.
- Delete caches you no longer need, for example from the repository's Actions cache page or with
gh cache delete.
Registry: tags and garbage collection
The registry backend writes to one tag, such as buildcache. Each export replaces that tag. The previous layer blobs are no longer referenced, but they still use storage until the registry removes them.
Two details matter here:
- Use
image-manifest=trueandoci-mediatypes=truewhen exporting, as in the earlier chapter. The cache is then stored as an image manifest, so registry tools that understand images can see it. - On Amazon ECR, add a lifecycle policy that expires untagged images. Without
image-manifest=true, the cache uses an index format that some lifecycle policies handle poorly.
For a self-hosted registry, run its garbage collector on a schedule, and run it when no pushes are in progress.
S3: lifecycle rules
S3 never expires objects by itself. A lifecycle rule is the standard fix. The s3 backend has an option that makes this safe: touch_refresh. It updates the timestamp of unchanged blobs at a set interval (24 hours by default). Blobs that are still in use keep getting newer, and only abandoned ones age out.
aws s3api put-bucket-lifecycle-configuration \
--bucket my-build-cache \
--lifecycle-configuration file://lifecycle.json{
"Rules": [
{
"ID": "expire-build-cache",
"Status": "Enabled",
"Filter": { "Prefix": "" },
"Expiration": { "Days": 14 },
"AbortIncompleteMultipartUpload": { "DaysAfterInitiation": 2 }
}
]
}This rule deletes objects 14 days after their last write. Because touch_refresh keeps active blobs fresh, only unused ones are removed. The multipart rule clears the leftovers of interrupted uploads, which are billed but invisible in normal listings.
If you use a shared bucket, set Prefix to the cache's prefix instead of leaving it empty. Otherwise the rule can delete unrelated objects.
The IAM policy from the permissions chapter needs no change, because lifecycle rules run inside S3 and not as your build role. Don't add s3:DeleteObject to the CI role just for cleanup.
What a missing entry costs you
Removing cache never breaks a build. A layer whose cached data is gone is a cache miss, and BuildKit rebuilds it. The cost is time. After a cleanup, the first build can be slow. Run it with --progress=plain and check which steps lose their CACHED marker.
Exercise
A team uses an S3 cache bucket with a 14-day expiration rule. A service is rebuilt every day, yet its builds are cold every two weeks. Which backend option helps, and why?
Show answer
Set touch_refresh (or keep its 24-hour default). It refreshes the timestamp of unchanged blobs, so blobs that are still in use don't reach the 14-day expiration age and are not deleted.