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

Time:07-22

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?

CodePudding user response:

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

CodePudding user response:

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
  • Related