user_mda
user_mda

Reputation: 19378

Command to delete all pods in all kubernetes namespaces

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

Upvotes: 381

Views: 568428

Answers (21)

Niharika Srivastava
Niharika Srivastava

Reputation: 1

kubectl delete pod (name of your container )

Upvotes: -1

Bahadur Singh
Bahadur Singh

Reputation: 51

#Forcefully complete prune or delete. Assuming foo is our namespace

kubectl delete --all pods --namespace=foo --force

#To keep watch on their removal from the list

watch kubectl get pods -n foo

Upvotes: 3

Ajit Surendran
Ajit Surendran

Reputation: 777

if you have hpa, then scale down.

Upvotes: 0

Gabriel Talavera
Gabriel Talavera

Reputation: 101

If you want to delete pods in all namespaces just to have them restarted and you are aware that some of them will be recreated, I like the following for loop:

for i in $(kubectl get pods -A | awk '{print $1}' | uniq | grep -V NAMESPACE); do kubectl delete --all pods -n $i; done

Upvotes: 0

efdestegul
efdestegul

Reputation: 637

One line command to delete all pods in all namespaces.

kubectl get ns -o=custom-columns=Namespace:.metadata.name --no-headers | xargs -n1 kubectl delete pods --all -n

Upvotes: 2

bwolmarans
bwolmarans

Reputation: 31

It was hinted at above, but I just thought I would helpfully point out that the shortcut for "--all-namespaces" is "-A" that's with a capital A. HTH somebody. I've opened a PR to have this helpful hint added to the official Kubernetes Cheat Sheet.

Upvotes: 0

jason
jason

Reputation: 1525

kubectl delete daemonsets,replicasets,services,deployments,pods,rc,ingress --all --all-namespaces

to get rid of them pesky replication controllers too.

Upvotes: 150

M Tehseen Nawaz
M Tehseen Nawaz

Reputation: 61

steps to delete pv:

  1. delete all deployment and pods or resources related to that PV

     kubectl delete --all deployment -n namespace
    
     kubectl delete --all pod -n namespace
    
  2. edit pv

     kubectl edit pv pv_name  -n namespace
    
     remove kubernetes.io/pv-protection
    
  3. delete pv

     kubectl delete pv pv_name  -n namespace
    

Upvotes: 5

Eric Tune
Eric Tune

Reputation: 8228

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

Upvotes: 645

kenjebek
kenjebek

Reputation: 11

If you have multiple pod which are crashing or error and you want to delete them

kubectl delete pods --all -n | gep

Upvotes: 0

Shivam Ji Mishra
Shivam Ji Mishra

Reputation: 41

You can use kubectl delete pods -l dev-lead!=carisa or what label you have.

Upvotes: 3

Jude Zhu
Jude Zhu

Reputation: 31

Delete all PODs in all Namespace only (restart deployment)

 kubectl get pod -A -o yaml | kubectl delete -f -

Upvotes: 3

nilesh_101
nilesh_101

Reputation: 327

I tried commands from listed answers here but pods were stuck in terminating state.
I found below command to delete all pods from particular namespace if stuck in terminating state or you are not able to delete it then you can delete pods forcefully.

kubectl delete pods --all --grace-period=0 --force --namespace namespace

Hope it might be useful to someone.

Upvotes: 6

Sachin Mishra
Sachin Mishra

Reputation: 1183

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

Upvotes: 5

Mo...
Mo...

Reputation: 1961

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.

Upvotes: 86

Braconnot_P
Braconnot_P

Reputation: 179

kubectl delete po,ing,svc,pv,pvc,sc,ep,rc,deploy,replicaset,daemonset --all -A

Upvotes: 2

user2218085
user2218085

Reputation: 77

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

Upvotes: 0

Emre Odabaş
Emre Odabaş

Reputation: 499

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

Upvotes: 0

Claudio Fahey
Claudio Fahey

Reputation: 800

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 {}

Upvotes: 2

Smaillns
Smaillns

Reputation: 3127

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

Upvotes: 0

Weike
Weike

Reputation: 1270

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.

Upvotes: 17

Related Questions