gh-actions-cache icon indicating copy to clipboard operation
gh-actions-cache copied to clipboard

Delete all caches or support bulk cache deletion using filters

Open RSickenberg opened this issue 1 year ago • 11 comments

Describe the feature or problem you’d like to solve

It would be cool to add a flag if we want to delete all cache from a repo or with some filters like older than x, bigger than x, and so on.

Proposed solution

The solution would help to gain time between the list command and the delete command.

Regards :)

RSickenberg avatar Jul 29 '22 08:07 RSickenberg

Hi @RSickenberg 👋🏽

Thank you for your wonderful suggestion! :)

Currently we are planning for some enhancements on the current CLI on making it better and I've added your suggestion in list. We will prioritise based on the urgent needs and feedback from other users.

In the meantime, in case you wish to contribute, please feel free to raise a Pull Request for your suggested feature and we will be happy to review the same.

Thanks! :)

kotewar avatar Aug 01 '22 07:08 kotewar

Help try again okay it looks popular

Adisa101 avatar Oct 30 '22 19:10 Adisa101

In case it's useful to anyone, I was able to do this with this snippet:

gh actions-cache list --limit 100 | tail -n +5 | awk '{print $1}' | tr '\n' '\0' | xargs -0 -n1 gh actions-cache delete --confirm

Unfortunately, it's kinda slow. Took a couple minutes to delete 250 cache entries.

tbenthompson avatar Nov 03 '22 22:11 tbenthompson

Any update @kotewar?

I'd love to see this progress soon. My use case is that I would like to delete the cache on working branches if they are older than x days - thus leaving them in place for working branches until after they merge. Still, I need to leave them in case the branch gets changed after an initial merge to a protected branch such as staging or main.

So I really need to be able to filer by x hours/days and also filter by "branch != main, staging, other-protected-branches." - this does not need to be branches that are listed under branch protection rules per se, I am fine with specifying them in the workflow. if I had this, I think I could get all branches older than X days. Then I could filter out the ones that have my protected branch names with a pipe to grep and a well-crafted regex. But it would also be great to have a negative filter on branch name(s) as well. I don't see an issue that asks for that but is it in your work plan? If not, I can open an issue (or point me to one and I will comment/upvote it)

The concept is this "get rid of all branches older than X that are not on branches main, staging, XYZ" and without having to get into scripting, etc.

tpak avatar Jan 30 '23 23:01 tpak

Hey @tpak, 👋🏽

Unfortunately no update yet. This kind of functionality needs support from the backend side and we don't have any APIs in place yet that support bulk deletion of caches which can make this faster than the alternative suggested above.

I'll add this request in our backlog so that we can support bulk deletion of caches given the filters.

kotewar avatar Jan 31 '23 11:01 kotewar

Thanks for the update @kotewar

I guess I need bulk delete but as I re-read it, I'm seeking to filter better on dates both for list and delete operations.

Unfortunately, as it currently stands there is no filtering capability for the list or delete operations. For delete, it would translate as "bulk" but for list it would materialize as a filter. The return from list operations "-d627a4016d488c214... 149.16 MB refs/heads/staging 16 hours ago" is not great to work with since it switches to " 1 days ago" after 24 hours. The solution above suggested by @tbenthompson does not filter although can be cleverly piped to another awk or grep step with regex.

I'll re-read existing issues and maybe open a new one for list filters

tpak avatar Jan 31 '23 16:01 tpak

@tpak , list capability does support filtering by cache name, branch and sorting by last-used, size, created-at. You can provide filters with the list command as shown in the documentation.

Although I understand what you are trying to refer here. I went through your recently created issues https://github.com/actions/gh-actions-cache/issues/51 and https://github.com/actions/gh-actions-cache/issues/52. We will add these to our backlog.

kotewar avatar Feb 01 '23 04:02 kotewar

Thanks @kotewar. Yes, I am aware of the current filter and sort options, but unfortunately, as outlined in 51 and 52, the current filter and sort capabilities are not quite up to the task at hand. Those will make great upgrades.

tpak avatar Feb 01 '23 06:02 tpak

For anyone stumbling on the same issue, another option to gh actions-cache by tbenthompson (which requires gh extension install actions/gh-actions-cache), you can also use the built-in gh cache list.

arminrosu avatar Aug 31 '23 13:08 arminrosu

I came across this issue while looking how to delete all caches, didn't see it mentioned here but the github cli now supports this via:

gh cache delete --all

rosshettel avatar Oct 02 '23 16:10 rosshettel

I had the same problem as OP and came up with the following command to delete cache entries that have not been accessed after a certain date:

gh cache list -R OWNER/REPO | awk '$5 < "2023-11-28"' | cut -f2 | xargs -I {} gh cache delete -R OWNER/REPO {}

You can even set the following alias which is more handy:

gh alias set --shell 'cache prune' 'gh cache list -R "$1" | awk -v date="$2" '\''$5 < date'\'' | cut -f2 | xargs -I {} gh cache delete -R "$1" {}'

Then run the command with :

gh cache prune OWNER/REPO 2023-11-28

This also works with time:

gh cache prune OWNER/REPO 2023-11-28T12:00

This last example will delete cache objects last accessed before Nov. 28 2023 at 12:00 (UTC) on the given repo.

Note that I use the last access date but if you want to filter on the cache object creation date you can just replace $5 with $4 in these commands.

Hope this helps!

aliberts avatar Nov 28 '23 14:11 aliberts

In continue to @tbenthompson suggestion, I used the following snippet to delete cache files that contain certain keywords: gh cache list --limit 100 | grep "keyword" | awk '{print $1}' | xargs -I {} gh cache delete {}

davseve avatar Apr 15 '24 06:04 davseve