0
votes

I know I can list all helm release using helm ls --tiller-namespace <tiller-namespace>

What command I can use for deleting helm release old than 1 month ?

1
Duplicated question. Answer could be found there stackoverflow.com/a/58301975/2110663Panoptik
@Panoptik that answer does not answer my question actually.Serena Xu

1 Answers

1
votes

You could use the below shell script, that takes all the list of releases and it's last deployed in seconds using the helm ls and jq utility command; which then loops through the releases list and does some subtraction on the number of days it has been deployed and then deletes the releases which are older than a month. By month, I've just given 30 days.

#!/bin/bash
#Store the release names alone for a specific tiller.
helm_releases=(`helm ls --short --tiller-namespace "kube-system"`)
#Store current date
CURRENT_TIME_SECONDS=`date '+%s'`
for RELEASE in ${helm_releases[@]};
do
  LAST_DEPLOYED_SECONDS=`helm status $RELEASE --tiller-namespace "kube-system" --output=json | jq -r '.info.last_deployed.seconds'`
  SEC_DIFF=`expr $CURRENT_TIME_SECONDS - $LAST_DEPLOYED_SECONDS`
  DAY_DIFF=`expr $SEC_DIFF / 86400`
  if [ "$DAY_DIFF" -gt 30 ]; then
    echo "$RELEASE is older than a month. Proceeding to delete it."
    helm delete --purge --no-hooks $RELEASE
  fi
done

You can still define your own logic on top of this by calculating the seconds differences for a month. Please note that I've explicitly mentioned the --tiller-namespace. You can use that if you're releases are deployed in a namespace which uses tiller other than kube-system.