Skip to content

Commit 63db812

Browse files
Skip already-deleted caches in PR cleanup loop (#554)
GitHub's Actions Cache list endpoint is eventually consistent: a cache just deleted in one outer iteration can reappear in the next list call, causing the second `gh cache delete` to 404 and fail the job (as seen in PR #553's Cleanup caches run). Track every ID the script has already removed and filter subsequent list responses against it so each cache is only deleted once.
1 parent c6bf118 commit 63db812

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

mise-tasks/github/cache/remove-for-pr.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,23 @@ def remove_cache(entry: CacheEntry) -> None:
5555
def main() -> None:
5656
pr = int(os.environ["usage_pr"]) # noqa: SIM112
5757
print(f"Removing caches for PR {pr}")
58+
seen: set[int] = set()
5859
counter = 0
5960
total_count = 0
6061
while True:
61-
caches = list_caches(pr)
62+
raw = list_caches(pr)
63+
caches = [c for c in raw if c.id not in seen]
6264
if not caches:
6365
print(f"Removed {counter} caches for PR {pr}")
6466
break
6567
total_count += len(caches)
66-
total_str = str(total_count)
67-
if len(caches) == LIMIT:
68-
total_str += "+"
68+
total_str = f"{total_count}+" if len(raw) == LIMIT else str(total_count)
6969

7070
for cache in caches:
7171
counter += 1
7272
print(f"Removing cache: {counter}/{total_str}: {cache.key}")
7373
remove_cache(cache)
74+
seen.add(cache.id)
7475

7576

7677
if __name__ == "__main__":

0 commit comments

Comments
 (0)