How to delete completed kubernetes pod? How to delete completed kubernetes pod? kubernetes kubernetes

How to delete completed kubernetes pod?


You can do this a bit easier, now.

You can list all completed pods by:

kubectl get pod --field-selector=status.phase==Succeeded

And delete all completed pods by:

kubectl delete pod --field-selector=status.phase==Succeeded


If this pods created by CronJob, you can use spec.failedJobsHistoryLimit and spec.successfulJobsHistoryLimit

Example:

apiVersion: batch/v1beta1kind: CronJobmetadata:  name: my-cron-jobspec:  schedule: "*/10 * * * *"  failedJobsHistoryLimit: 1  successfulJobsHistoryLimit: 3  jobTemplate:    spec:      template:         ...


You can do it on two ways.

$ kubectl delete pod $(kubectl get pods | grep Completed | awk '{print $1}')

or

$ kubectl get pods | grep Completed | awk '{print $1}' | xargs kubectl delete pod

Both solutions will do the job.