|
| 1 | +--- |
| 2 | +title: Managing caches |
| 3 | +intro: 'You can monitor, filter, and delete dependency caches created from your workflows.' |
| 4 | +versions: |
| 5 | + fpt: '*' |
| 6 | + ghes: '*' |
| 7 | + ghec: '*' |
| 8 | +shortTitle: Manage caches |
| 9 | +allowTitleToDifferFromFilename: true |
| 10 | +--- |
| 11 | + |
| 12 | +This article describes managing caches with the {% data variables.product.prodname_dotcom %} web interface, but you can also manage them: |
| 13 | + |
| 14 | +* Using the REST API. See [AUTOTITLE](/rest/actions/cache). |
| 15 | +* With the `gh cache` subcommand from the command line. See the [GitHub CLI documentation](https://cli.github.com/manual/gh_cache). |
| 16 | + |
| 17 | +## Viewing cache entries |
| 18 | + |
| 19 | +You can use the web interface to view a list of cache entries for a repository. In the cache list, you can see how much disk space each cache is using, when the cache was created, and when the cache was last used. |
| 20 | + |
| 21 | +{% data reusables.repositories.navigate-to-repo %} |
| 22 | +{% data reusables.repositories.actions-tab %} |
| 23 | +{% data reusables.repositories.actions-cache-list %} |
| 24 | +1. Review the list of cache entries for the repository. |
| 25 | + |
| 26 | + * To search for cache entries used for a specific branch, click the **Branch** dropdown menu and select a branch. The cache list will display all of the caches used for the selected branch. |
| 27 | + * To search for cache entries with a specific cache key, use the syntax `key: key-name` in the **Filter caches** field. The cache list will display caches from all branches where the key was used. |
| 28 | + |
| 29 | +  |
| 30 | + |
| 31 | +## Deleting cache entries |
| 32 | + |
| 33 | +Users with `write` access to a repository can use the {% data variables.product.prodname_dotcom %} web interface to delete cache entries. |
| 34 | + |
| 35 | +{% data reusables.repositories.navigate-to-repo %} |
| 36 | +{% data reusables.repositories.actions-tab %} |
| 37 | +{% data reusables.repositories.actions-cache-list %} |
| 38 | +1. To the right of the cache entry you want to delete, click {% octicon "trash" aria-label="Delete cache" %}. |
| 39 | + |
| 40 | +  |
| 41 | + |
| 42 | +## Force deleting cache entries |
| 43 | + |
| 44 | +Caches have branch scope restrictions in place, which means some caches have limited usage options. For more information on cache scope restrictions, see [AUTOTITLE](/actions/reference/dependency-caching-reference#restrictions-for-accessing-a-cache). If caches limited to a specific branch are using a lot of storage quota, it may cause caches from the `default` branch to be created and deleted at a high frequency. |
| 45 | + |
| 46 | +For example, a repository could have many new pull requests opened, each with their own caches that are restricted to that branch. These caches could take up the majority of the cache storage for that repository. {% data reusables.actions.cache-eviction-policy %} In order to prevent cache thrashing when this happens, you can set up workflows to delete caches on a faster cadence than the cache eviction policy will. You can use the {% data variables.product.prodname_cli %} to delete caches for specific branches. |
| 47 | + |
| 48 | +The following example workflow uses `gh cache` to delete up to 100 caches created by a branch once a pull request is closed. |
| 49 | + |
| 50 | +To run the following example on cross-repository pull requests or pull requests from forks, you can trigger the workflow with the `pull_request_target` event. If you do use `pull_request_target` to trigger the workflow, there are security considerations to keep in mind. For more information, see [AUTOTITLE](/actions/using-workflows/events-that-trigger-workflows#pull_request_target). |
| 51 | + |
| 52 | +```yaml |
| 53 | +name: Cleanup github runner caches on closed pull requests |
| 54 | +on: |
| 55 | + pull_request: |
| 56 | + types: |
| 57 | + - closed |
| 58 | + |
| 59 | +jobs: |
| 60 | + cleanup: |
| 61 | + runs-on: ubuntu-latest |
| 62 | + permissions: |
| 63 | + actions: write |
| 64 | + steps: |
| 65 | + - name: Cleanup |
| 66 | + run: | |
| 67 | + echo "Fetching list of cache keys" |
| 68 | + cacheKeysForPR=$(gh cache list --ref $BRANCH --limit 100 --json id --jq '.[].id') |
| 69 | +
|
| 70 | + ## Setting this to not fail the workflow while deleting cache keys. |
| 71 | + set +e |
| 72 | + echo "Deleting caches..." |
| 73 | + for cacheKey in $cacheKeysForPR |
| 74 | + do |
| 75 | + gh cache delete $cacheKey |
| 76 | + done |
| 77 | + echo "Done" |
| 78 | + env: |
| 79 | + GH_TOKEN: {% raw %}${{ secrets.GITHUB_TOKEN }}{% endraw %} |
| 80 | + GH_REPO: {% raw %}${{ github.repository }}{% endraw %} |
| 81 | + BRANCH: refs/pull/{% raw %}${{ github.event.pull_request.number }}{% endraw %}/merge |
| 82 | +``` |
| 83 | +
|
| 84 | +Alternatively, you can use the API to automatically list or delete all caches on your own cadence. For more information, see [AUTOTITLE](/rest/actions/cache#about-the-cache-in-github-actions). |
0 commit comments