If we've done something wrong with the pod, or it may have broken for some reason, there is a simple way to delete a pod using the kubectl delete pod command:
$ kubectl delete pod httpd-8576c89d7-qjd62 pod "httpd-8576c89d7-qjd62" deleted
We can delete all pods using the --all option:
$ kubectl delete pod --all pod "httpd-8576c89d7-qjd62" deleted pod "httpd1-c9f7d7fd9-rn2nz" deleted pod "httpd2-5b4ff5cf57-vlhb4" deleted
Note that if you run kubectl get pods, you will see all the containers running again. The reason for this is that, when we run the kubectl run command, it creates several different Kubernetes resources, which we are going to discuss in the following section.
We can delete Kubernetes resources by running kubectl delete all with the -l option:
$ kubectl delete all -l app=httpd-demo1 deployment "httpd1" deleted pod "httpd1-c9f7d7fd9-d9w94" deleted
$ kubectl get pods NAME READY STATUS RESTARTS AGE httpd-8576c89d7-qjd62 1/1 Running 0 17m httpd2-5b4ff5cf57-9llkn 1/1 Running 0 15s
This command will delete all Kubernetes with a httpd-demo1 label only. The other two pods will be still available.
Alternatively, we can delete all Kubernetes resources we have created so far by running the kubectl delete all --all command:
$ kubectl delete all --all deployment "httpd" deleted deployment "httpd2" deleted pod "httpd-8576c89d7-ktnwh" deleted pod "httpd2-5b4ff5cf57-t58nd" deleted service "kubernetes" deleted service "nginx-exposed" deleted