Helm delete all release older than some date, updated before some date or if the app version is lower than

10/9/2019

How can I delete release older than, for example, the 1st of October? I mean updated before the 1st of October.
Alternatively, delete all releases with the app version lower than _.

Helm ls output:

|NAME|REVISION|UPDATED |STATUS |CHART |APP VERSION|NAMESPACE|
|myapp| 8 |Fri Sep 27 17:27:20 2019|DEPLOYED|myapp.85|85 |default|

The following command deletes just one.

helm delete relase_name

The following is not a great solution as well

helm delete relase_name1 relase_name2 relase_name3

Note1: I don't want to delete all. There is an explanation of how to do that over here Helm delete all releases and I don't want to do that. However, I assume I need to use bash for this task.

Note2: I have already read documentation it is not that big. There is nothing over there about filtering. https://helm.sh/docs/helm/#helm-delete

Note3: I have already looked into helm sources, I am not 100% sure but It doesn't look like it is possible https://github.com/helm/helm/tree/master/pkg/helm

Thank you in advance!

-- Sergii Zhuravskyi
kubernetes
kubernetes-helm

2 Answers

5/25/2020

Assuming Helm version 3+

You can use jq and xargs to accomplish this:

Question 1, to delete releases that were last updated before $TIMESTAMP (in seconds):

helm ls -o json | jq -r --argjson timestamp $TIMESTAMP '.[] | select (.updated | sub("\\..*";"Z") | sub("\\s";"T") | fromdate < now - $timestamp).name' | xargs -L1 helm uninstall

sub("\\..*";"Z") | sub("\\s";"T") converts the date format Helm uses in their output to ISO 8601.

Question 2, to delete releases with an app version older than $VERSION:

helm ls -o json | jq -r --argjson version $VERSION '.[] | select(.app_version | sub("\\.[0-9]
quot;
;"") | tonumber | . < $version).name' | xargs -L1 helm uninstall

$VERSION should be major or major.minor only (i.e. 2 or 2.1). don't use a patch number.

Question 3, to delete releases by their initial deploy date, you'll have to parse the contents of the helm history RELEASE command for each release.

I won't solve this one, but it would look something like:

loop over helm ls
  get release name
  get first entry of helm history for that release
  pass to jq and process like the answer for question 1

Relevant Links:

-- ericfossas
Source: StackOverflow

10/9/2019

The command bellow kind of worked for me, it is far from perfect solution but at least it helped me :

helm ls -d -m 25 --namespace default --short | xargs -L1 helm delete

helm ls - lists all of the releases.
-d orders by date.
-m maximum number of releases to fetch (so I take 25).
--namespace default - Show releases within a specific namespace, this option helped me to filter my app releases.
--short - this option limits output of the command to release names only.

The solution is not perfect and I hope that someone can provide a better solution.

-- Sergii Zhuravskyi
Source: StackOverflow