Command to delete all pods in all kubernetes namespaces
There is no command to do exactly what you asked.
Here are some close matches.
Be careful before running any of these commands. Make sure you are connected to the right cluster, if you use multiple clusters. Consider running. kubectl config view
first.
You can delete all the pods in a single namespace with this command:
kubectl delete --all pods --namespace=foo
You can also delete all deployments in namespace which will delete all pods attached with the deployments corresponding to the namespace
kubectl delete --all deployments --namespace=foo
You can delete all namespaces and every object in every namespace (but not un-namespaced objects, like nodes and some events) with this command:
kubectl delete --all namespaces
However, the latter command is probably not something you want to do, since it will delete things in the kube-system namespace, which will make your cluster not usable.
This command will delete all the namespaces except kube-system, which might be useful:
for each in $(kubectl get ns -o jsonpath="{.items[*].metadata.name}" | grep -v kube-system);
do
kubectl delete ns $each
done
You can simply run
kubectl delete all --all --all-namespaces
The first
all
means the common resource kinds (pods, replicasets, deployments, ...)kubectl get all == kubectl get pods,rs,deployments, ...
The second
--all
means to select all resources of the selected kinds
Note that all
does not include:
- non namespaced resourced (e.g., clusterrolebindings, clusterroles, ...)
- configmaps
- rolebindings
- roles
- secrets
- ...
In order to clean up perfectly,
- you could use other tools (e.g., Helm, Kustomize, ...)
- you could use a namespace.
- you could use labels when you create resources.
You just need sed
to do this:
kubectl get pods --no-headers=true --all-namespaces |sed -r 's/(\S+)\s+(\S+).*/kubectl --namespace \1 delete pod \2/e'
Explains:
- use command
kubectl get pods --all-namespaces
to get the list of all pods in all namespaces. - use
--no-headers=true
option to hide the headers. - use
s
command ofsed
to fetch the first two words, which representnamespace
andpod's name
respectively, then assemble thedelete
command using them. - the final
delete
command is just like:kubectl --namespace kube-system delete pod heapster-eq3yw
. - use the
e
modifier ofs
command to execute the command assembled above, which will do the actualdelete
works.
To avoid delete pods in kube-system
namespace, just need to add grep -v kube-system
to exclude kube-system
namespace before the sed
command.
kubectl delete daemonsets,replicasets,services,deployments,pods,rc,ingress --all --all-namespaces
to get rid of them pesky replication controllers too.