kubernetesterminal

How to delete all the Terminated pods of a kubernetes cluster?


I know how to delete a specific pod:

kubectl -n <namespace> delete pod <pod-name>

Is there a way to delete all the Terminated pods once?

enter image description here


Solution

  • What does terminated pod mean? If you wish to delete finished pods of any jobs in the namespace then you can remove them with a single command:

    kubectl -n <namespace> delete pods --field-selector=status.phase==Succeeded
    

    Another approach in Kubernetes 1.23 onwards is to use Job's TTL controller feature:

    spec:
      ttlSecondsAfterFinished: 100
    

    In your case Terminated status means your pods are in a failed state. To remove them just change the status.phase to Failed state (https://kubernetes.io/docs/reference/kubernetes-api/workload-resources/pod-v1/#PodStatus)