LoginSignup
4

GitHub Actions の "actions/cache" で作成した cache を GitHub CLI で削除する

Last updated at Posted at 2022-08-26

はじめに

  • GitHub Actions の "actions/cache" で作成した cache を GitHub CLI で削除する 方法についてまとめました

事前準備

  • この方法は GitHub CLI が使える前提です。使えない場合は設定するか、 "curl" で実行したい人は docs を参照してください

cache を削除する

Cache Key を取得する

GitHub Actions の実行結果を確認し、Cache Key を取得します

以下のような Log が出力されている箇所を探します
多く場合 Run actions/cache@v2Post Run actions/cache@v2 等といった Job 内から探すことが出来ます

Run actions/cache@v2
Received XXXXXX of XXXXXX (100.0%), 156.5 MBs/sec
Cache Size: ~134 MB (140003694 B)
/bin/tar --use-compress-program zstd -d -xf /home/runner/work/_temp/xxxx-xxxx-xxxx-xxxx-xxxx/cache.tzst -P -C /home/runner/work/xxxx/xxxx
Cache restored successfully
Cache restored from key: Linux-xxxx-xxxx-xxxx-cache-all-2704376682
Post Run actions/cache@v2
Post job cleanup.
/bin/tar --posix --use-compress-program zstd -T0 -cf cache.tzst -P -C /home/runner/work/xxxx/xxxx --files-from manifest.txt
Cache Size: ~134 MB (140003694 B)
Cache saved successfully
Cache restored from key: Linux-xxxx-xxxx-xxxx-cache-all-2704376682

上記ログであれば、 Cache kye は Linux-xxxx-xxxx-xxxx-cache-all-2704376682 が cache key となります

Cache を削除する

直前のステップで取得した Cache key を用い Cache を削除します

gh api --method DELETE -H "Accept: application/vnd.github+json" "/repos/USER_OR_ORG/REPONAME/actions/caches?key=YOUR_CACHE_KEY"

YOUR_CACHE_KEY 部分を Cache key に置き換えて実行してください

実行例

実際に Cache を削除してみます
今回は、mziyut/test-github-actions repository に作成した cahce を削除しました

 gh api --method DELETE -H "Accept: application/vnd.github+json" "/repos/mziyut/test-github-actions/actions/caches?key=Linux-xxxx-xxxx-xxxx-cache-all-2704376682"
{
  "total_count": 1,
  "actions_caches": [
    {
      "id": 22830,
      "ref": "refs/pull/xxxx/xxxx",
      "key": "Linux-xxxx-xxxx-xxxx-cache-all-2704376682",
      "version": "xxxxxxx",
      "last_accessed_at": "2022-xx-xxTxx:xx:xx.xxxxxxxxZ",
      "created_at": "2022-xx-xxTxx:xx:xx.xxxxxxxxZ",
      "size_in_bytes": 140003694
    }
  ]
}

削除出来ているか確認するため、同じ cache key に対して再度 cache の削除してみます

gh api --method DELETE -H "Accept: application/vnd.github+json" "/repos/mziyut/test-github-actions/actions/caches?key=Linux-xxxx-xxxx-xxxx-cache-all-2704376682"
{
  "message": "Not Found",
  "documentation_url": "https://docs.github.com/rest/actions/cache#delete-github-actions-caches-for-a-repository-using-a-cache-key"
}
gh: Not Found (HTTP 404)

Cache が存在しないため、 Not Found (HTTP 404) が返却されました

Reference

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4