26

Is it possible to delete POD in kubernetes based on creation time or age?

Example : I would like to delete all PODs which are older than 1 day. These PODs are orphaned , therefore no new PODs will be created.

dansl1982
  • 779
  • 1
  • 6
  • 9

5 Answers5

41

This command will delete all PODs older than one day :

kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}} {{.metadata.creationTimestamp}}{{"\n"}}{{end}}' | awk '$2 <= "'$(date -d 'yesterday' -Ins --utc | sed 's/+0000/Z/')'" { print $1 }' | xargs --no-run-if-empty kubectl delete pod

This command will delete all PODs older than 4 hours :

kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}} {{.metadata.creationTimestamp}}{{"\n"}}{{end}}' | awk '$2 <= "'$(date -d'now-4 hours' -Ins --utc | sed 's/+0000/Z/')'" { print $1 }' | xargs --no-run-if-empty kubectl delete pod
marcind
  • 52,524
  • 13
  • 123
  • 111
dansl1982
  • 779
  • 1
  • 6
  • 9
  • 3
    Thanks for your answer! A couple of observations: First of all, this needs GNU `date`, so if for example you're running this inside an alpine container it will fail unless you install that version of `date`. My version of kubectl was outputting `+00:00` instead of `+0000`. Also, nanoseconds don't seem to be necessary. – Aldo 'xoen' Giambelluca May 01 '19 at 11:26
  • 2
    The above command requires GNU version of xargs and date, which can be installed on macOS from HomeBrew as gxargs and gdate respectively. `brew install findutils coreutils` – Hang Aug 23 '20 at 22:46
  • 1
    Thanks for this snippet! Super helpful :) – Eric Sep 23 '20 at 20:16
  • 1
    can anybody explain how this comparison is working/valid `$2 <= "'$(date -d 'yesterday' -Ins --utc ` ? – alixander Oct 01 '20 at 10:26
  • I like this solution a lot but it looks like the `date -d 'yesterday' -Ins --utc` part does not work on macOS sadly, probably because macOS is BSD based but the answer is for standard GNU. Anyone have a solution for macOS? – Michael Butler Jan 15 '21 at 18:01
  • 1
    @MichaelButler See [the answer](https://stackoverflow.com/a/66479725/8431936) below by SQLesion. The main thing is to use `gdate` on macOS which is the GNU version of `date`. – Jack Kawell Nov 18 '21 at 18:33
23

We could do this with awk by doing a regex [0-9]+d directly on the AGE ($5, 5th column) column and then printing the corresponding NAME ($1, first column) column

kubectl delete pod $(kubectl get pod | awk 'match($5,/[0-9]+d/) {print $1}')

Test first to see what's matching:

kubectl get pod | awk 'match($5,/[0-9]+d/) {print $0}'

$0 means all columns

should_be_working
  • 559
  • 1
  • 4
  • 11
  • 1
    For me on kubectl 1.18.15 the right column to use is `$4` not `$5`. The spirit of the answer is correct though and I'm hesitant to edit it in case it's my fault, not the answer's – 2rs2ts Mar 30 '21 at 22:28
  • Any idea on how to do this in powershell? – Oplop98 Dec 17 '21 at 16:54
  • I'm sure this is straightforward to most, but don't forget to use namespaces on both the "delete" and "get" statements if you use them. – jtclaypool Mar 11 '22 at 15:58
4

You can either add a liveness probe to track how long the pod alive and kill it when it's longer a certain period. Or you can schedule a CronJob

Cindy
  • 234
  • 2
  • 5
  • 3
    For liveness probe, you can add `/bin/sh -c "touch /tmp/healthy; sleep [time you want the container to alive]; rm -rf /tmp/healthy; sleep 600"` in the args. I will kill the old container and restart a new one when the time reaches. – Cindy Feb 22 '18 at 22:34
  • hi Cindy , First of all thank you for the answer, you suggestion is working but it looks like kubectl restarts a container and not deleting a POD. – dansl1982 Feb 23 '18 at 08:44
  • 1
    I found it easiset to actually run the script from [dansl1982's answer](https://stackoverflow.com/a/48960060/121660) inside a cron job like [this one](https://stackoverflow.com/a/54908449/121660) – captncraig Apr 30 '19 at 20:44
2

modified to work on mac:

# try the gnu versions: gxargs
brew install findutils coreutils


kubectl get pods -o go-template -n gui2 --template '{{range .items}}{{.metadata.name}} {{.metadata.creationTimestamp}}{{"\n"}}{{end}}' | awk '$2 <= "'$(gdate -d '21 days ago' -Ins --utc | sed 's/+0000/Z/')'" { print $1 }' | gxargs --no-run-if-empty kubectl delete pod
SQLesion
  • 155
  • 1
  • 9
2

Here is an alternative answer that uses kubectl, jq, and xargs.

It works locally for me on Mac, but I also uses it in a Kubernetes cronjob that runs debian-slim, so I think it works on both Mac and Linux.

The time is set in seconds (86400s here for 1 day).

kubectl get pod -o json | jq -r --argjson timestamp 86400 '.items[] | select (.metadata.creationTimestamp | sub("\\..*";"Z") | sub("\\s";"T") | fromdate < now - $timestamp).metadata.name' | xargs -r -L1 kubectl delete pod ;
ericfossas
  • 331
  • 4
  • 6