Command to delete all pods in all kubernetes namespaces

11/3/2015

Upon looking at the docs, there is an API call to delete 'a' pod, but is there a way to kill all pods in all namespaces?

-- user_mda
kubernetes
rest

10 Answers

7/6/2019

Kubectl bulk (bulk-action on krew) plugin may be useful for you, it gives you bulk operations on selected resources. This is the command for deleting pods

 ' kubectl bulk pods -n namespace delete '

You could check details in this

-- Emre Odabaş
Source: StackOverflow

3/30/2020

K8s completely works on the fundamental of the namespace. if you like to release all the resource related to specified namespace.

you can use the below mentioned :

kubectl delete namespace k8sdemo-app

-- Sachin Mishra
Source: StackOverflow

4/12/2019

If you already have pods which are recreated, think to delete all deployments first

kubectl delete -n *NAMESPACE deployment *DEPLOYMENT

Just replace the NAMSPACE and the DEPLOYMENT to corresponding ones, you can get all deployments information by the following command

kubectl get deployments --all-namespaces
-- Smaillns
Source: StackOverflow

8/14/2019

I create a python code to delete all in namespace

delall.py

import json,sys,os;

obj=json.load(sys.stdin);
for item in obj["items"]:
        os.system("kubectl delete " + item["kind"] + "/" +item["metadata"]["name"] + " -n yournamespace")

and then

kubectl get all -n kong -o json | python delall.py
-- user2218085
Source: StackOverflow

11/3/2015

There is no command to do exactly what you asked.

Here are some close matches.

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
-- Eric Tune
Source: StackOverflow

5/17/2018
kubectl delete daemonsets,replicasets,services,deployments,pods,rc --all

to get rid of them pesky replication controllers too.

-- jason
Source: StackOverflow

5/16/2017

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:

  1. use command kubectl get pods --all-namespaces to get the list of all pods in all namespaces.
  2. use --no-headers=true option to hide the headers.
  3. use s command of sed to fetch the first two words, which represent namespace and pod's name respectively, then assemble the delete command using them.
  4. the final delete command is just like: kubectl --namespace kube-system delete pod heapster-eq3yw.
  5. use the e modifier of s command to execute the command assembled above, which will do the actual delete 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.

-- Weike
Source: StackOverflow

3/8/2020

You can simply run

kubectl delete all --all --all-namespaces
-- Mo K
Source: StackOverflow

4/25/2019

Here is a one-liner that can be extended with grep to filter by name.

kubectl get pods -o jsonpath="{.items[*].metadata.name}" | \
tr " " "\n" | \
xargs -i -P 0 kubectl delete pods {}
-- Claudio Fahey
Source: StackOverflow

3/28/2020
kubectl delete po,ing,svc,pv,pvc,sc,ep,rc,deploy,replicaset,daemonset --all -A
-- Braconnot_P
Source: StackOverflow