KaliTheGreat
KaliTheGreat

Reputation: 669

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

Upvotes: 0

Views: 1517

Answers (2)

Szczad
Szczad

Reputation: 836

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)

Upvotes: 4

Jean-Pascal J.
Jean-Pascal J.

Reputation: 315

You can pipe 2 commands:

kubectl -n <namespace> get pods --field-selector=status.phase==Succeeded -o custom-columns=NAME:.metadata.name  --no-headers | kubectl -n <namespace> delete pods

I don't think there is an 'exec' option to kubectl get (like the CLI tool 'find' for instance). If the command fits your needs, you can always convert it to an alias or shell function

Upvotes: 1

Related Questions