kubectl delete pod with deployment name

Viewed 1299

I often sometimes my pods for debugging with kubectl delete pod, so it updates the container image. But every time I have to lookup the complete name of the pod:

kubectl delete pod deployment_name-56fccbbfb8g4rj6

How can I just delete the pod without knowing the hash?

4 Answers

You can delete the pod based on the labels. For example, if your pod has label app=alpine, you can delete it like this:

kubectl delete pod -l app=alpine

Depending on your shell, you can use auto completion to have your pod name suggested by the system.

  • Bash-completion is provided by many package managers (see here). You can install it with apt-get install bash-completion or yum install bash-completion, etc.

  • For Zsh this can be generated with the command kubectl completion zsh. Sourcing the completion script in your shell enables kubectl autocompletion.

    To do so in all your shell sessions, add the following to your ~/.zshrc file:

    source <(kubectl completion zsh)
    
    

Another thing is that you don't need to destroy your pod to update your image. You can simply use kubectl patch or kubectl set image.

For more reading please check Kubernetes document about patching the API objects with kubectl.

  • delete pods / namesapces

kubectl get pod --all-namespaces | grep -viE "compl|running" | awk 'NR>1{print $2 " -n " $1}' |while read line;do kubectl delete pod $line;done

that's not possible but you can delete all of them at once using kubectl here:

kubectl delete --all pods --namespace=foo

if you want to delete a single pod you have to do it manually. else try this solution.

Related