Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kubernetes: How to delete PODs based on age/creation time

Tags:

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.

like image 869
dansl1982 Avatar asked Feb 22 '18 18:02

dansl1982


People also ask

How do I delete specific pods in Kubernetes?

Destroy Pod The action of deleting the pod is simple. To delete the pod you have created, just run kubectl delete pod nginx . Be sure to confirm the name of the pod you want to delete before pressing Enter. If you have completed the task of deleting the pod successfully, pod nginx deleted will appear in the terminal.

Does deleting namespace delete pods?

Deleting a namespace sometimes leaves orphaned deployment and pods which can't be deleted #36891. area/api. area/apiserver area/HA lifecycle/rotten. priority/important-soon.


1 Answers

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 
like image 68
dansl1982 Avatar answered Dec 04 '22 06:12

dansl1982