361

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?

2240
  • 1,547
  • 2
  • 12
  • 30
user_mda
  • 18,148
  • 27
  • 82
  • 145

20 Answers20

611

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
Eric Tune
  • 7,819
  • 1
  • 15
  • 17
  • 4
    With the current version of k8s, the command "kubectl delete --all namespaces" does not delete the system stuff anymore... It says e.g.: namespaces "default" is forbidden: this namespace may not be deleted – Kai Wähner Apr 07 '18 at 09:33
  • Does this delete the deployment or just the pods? – Sinaesthetic Aug 31 '18 at 01:07
  • 3
    `kubectl delete pods --all --all-namespaces` seems to work sometimes (not sure what causes it to work in some environments and not in others). It works well from my bastion host but not from laptop. Both running debian and both running the same version of kubectl (cluster version 1.13) – Patrick W Oct 24 '19 at 20:37
  • @KaiWähner if I'm not mistaken the k8s system-related stuff are in the `kube-system` namespace. In the message example, I can see the `default` namespace. Is `kube-system` deleted with the command you have attached? – George Tseres Mar 28 '20 at 18:41
  • ```for each in``` very helpful – MechaCode Feb 11 '21 at 13:34
  • The regular namespace-delete command failed for me (the namespace contents were stuck in the "terminating" state forever), but this script fixed it: https://github.com/ctron/kill-kube-ns – Venryx Aug 11 '21 at 07:33
  • I wish I could go back in time and verify if my Kubernetes was linked to a production server before I ran this. Something like `kubectl config view`. Perhaps we could add some sort of warning before the answer? – antoineMoPa Aug 27 '21 at 22:42
  • 3
    kube-system, kube-public and default will not be deleted with `kubectl delete --all namespaces` – Ryler Hockenbury Sep 30 '21 at 19:01
  • 1
    @antoineMoPa added warning per your suggestion. – Eric Tune Oct 02 '21 at 00:13
  • Or you can see the output of `kubectl config view | grep namespace` and based on the result of the namespace, you specify it in the `--namespace` option. An example of a `default` namespace would be: `kubectl delete --all pods --namespace=default` – ezzeddin Jun 29 '22 at 12:47
  • does deleting a single namespace delete all the objects in that namespace? – D.B.K Apr 22 '23 at 18:45
141
kubectl delete daemonsets,replicasets,services,deployments,pods,rc,ingress --all --all-namespaces

to get rid of them pesky replication controllers too.

Taz
  • 5,755
  • 6
  • 26
  • 63
jason
  • 1,427
  • 1
  • 7
  • 2
  • 4
    This is wrong. This only deletes in the current namespace. – PleaseHelp Nov 28 '18 at 18:39
  • Add the -n flag, but sorry that's for one pod at a time and not all at once – grbonk Mar 15 '19 at 17:31
  • 2
    You are missing `ing` to also delete ingresses – Druska May 29 '19 at 18:10
  • 3
    you might want to add "statefulset" as well ( if applicable ). Otherwise it will keep forking new pods even after you run above command! – buch11 Jul 09 '19 at 04:35
  • Also remove `apiservice` else you end up with namespaces locked in the `Terminating` state because some apiserivces are attached to them. cf. https://stackoverflow.com/questions/52369247/namespace-stuck-as-terminating-how-i-removed-it?page=1&tab=createdasc#answer-58644787 – noraj Aug 25 '22 at 16:53
82

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.
Mo...
  • 1,812
  • 2
  • 17
  • 22
17

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
  • 1,232
  • 12
  • 15
6

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.

nilesh_101
  • 307
  • 3
  • 10
5

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
  • 1,125
  • 1
  • 16
  • 17
5

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
    
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
3

Delete all PODs in all Namespace only (restart deployment)

 kubectl get pod -A -o yaml | kubectl delete -f -
Jude Zhu
  • 31
  • 3
3

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

dippas
  • 58,591
  • 15
  • 114
  • 126
2

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
  • 730
  • 6
  • 7
  • `kubectl get pods --selector=app="${DEPLOYMENT_NAME}" -o=jsonpath='{.items[*].metadata.name}' | tr " " "\n" | xargs -n 1 kubectl delete pod` – Janux Apr 22 '21 at 16:49
2
kubectl delete po,ing,svc,pv,pvc,sc,ep,rc,deploy,replicaset,daemonset --all -A
Braconnot_P
  • 181
  • 3
  • 8
  • 4
    Be careful! As [PV is not namespaced](https://kubernetes.io/docs/reference/kubectl/overview/#resource-types) the above command will remove all PVs cluster-wide. – Ahmad Ahmadi May 17 '20 at 17:56
2

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
efdestegul
  • 617
  • 3
  • 6
1

#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

0

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
  • 2,540
  • 1
  • 28
  • 40
0

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

Chanaka Weerasinghe
  • 5,404
  • 2
  • 26
  • 39
Emre Odabaş
  • 409
  • 3
  • 6
0

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
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
0

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

kubectl delete pods --all -n | gep

kenjebek
  • 11
  • 3
0

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.

bwolmarans
  • 31
  • 3
0

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
0

if you have hpa, then scale down.

Ajit Surendran
  • 709
  • 7
  • 4