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.
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.
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
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
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
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
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 ;
Here's a variant of the answer by dansl1982 above that will delete all pods with a particular label in all namespaces.
kubectl get pods -A -l app=my-api -o go-template --template '{{range .items}}{{.metadata.namespace}} {{.metadata.name}} {{.metadata.creationTimestamp}}{{"\n"}}{{end}}' | awk '$3 <= "'$(date -d'now-4 hours' -Ins --utc | sed 's/+0000/Z/')'" { print $1 " " $2 }' | xargs -L 1 --no-run-if-empty kubectl delete pod -n $1 $2
No answer here by itself was complete for what I needed, so I took bits of (and upvoted) several answers, then mashed them together to delete completed pods older than a day, from various namespaces.
Here's what worked for me:
kubectl get pod --all-namespaces --field-selector=status.phase==Succeeded \
| awk 'match($6,/[0-9]+d/) {print $1, $2}' \
| xargs -L 1 --verbose --no-run-if-empty kubectl delete pod -n $1 $2
kubectl
to find for completed pods in all namespaces,awk
to filter pods that finished a day or more ago (any age string ending with a d
),xargs
to run (and output) kubectl delete
against each pod, specifying the pod's namespace.Here is another answer by showing the custom field when get pods
and filter by the .metadata.creationTimestamp
kubectl delete pod -n foo $(kubectl get pods -n foo --field-selector=status.phase=Failed -o=custom-columns=NAME:.metadata.name,AGE:.metadata.creationTimestamp --no-headers=true --sort-by=.metadata.creationTimestamp | awk '$2 <= "'$(gdate -d '10 days ago' -Ins | sed 's/+0000/Z/')'" { print $1 }')
Based on the above command -o=custom-columns=NAME:
has been used for the custom columns.