223

Include:

  • Daemon Sets
  • Deployments
  • Jobs
  • Pods
  • Replica Sets
  • Replication Controllers
  • Stateful Sets
  • Services
  • ...

If has replicationcontroller, when delete some deployments they will regenerate. Is there a way to make kubenetes back to initialize status?

0

9 Answers 9

445

Method 1: To delete everything from the current namespace (which is normally the default namespace) using kubectl delete:

kubectl delete all --all

all refers to all resource types such as pods, deployments, services, etc. --all is used to delete every object of that resource type instead of specifying it using its name or label.

To delete everything from a certain namespace you use the -n flag:

kubectl delete all --all -n {namespace}

Method 2: You can also delete a namespace and re-create it. This will delete everything that belongs to it:

kubectl delete namespace {namespace}
kubectl create namespace {namespace}

Note (thanks @Marcus): all in kubernetes does not refers to every kubernetes object, such as admin level resources (limits, quota, policy, authorization rules). If you really want to make sure to delete eveything, it's better to delete the namespace and re-create it. Another way to do that is to use kubectl api-resources to get all resource types, as seen here:

kubectl delete "$(kubectl api-resources --namespaced=true --verbs=delete -o name | tr "\n" "," | sed -e 's/,$//')" --all
2
  • 8
    This doesn't delete RoleBindings, ServiceAccounts or NetworkPolicy Feb 26, 2020 at 2:07
  • Remove Method 1 please - it doesn't do what it should. Method 2 is the correct answer.
    – Marc
    Dec 17, 2021 at 10:55
56

Kubernetes Namespace would be the perfect options for you. You can easily create namespace resource.

kubectl create -f custom-namespace.yaml

$  apiVersion: v1
    kind: Namespace
    metadata:
      name:custom-namespace

Now you can deploy all of the other resources(Deployment,ReplicaSet,Services etc) in that custom namespaces.

If you want to delete all of these resources, you just need to delete custom namespace. by deleting custom namespace, all of the other resources would be deleted. Without it, ReplicaSet might create new pods when existing pods are deleted.

To work with Namespace, you need to add --namespace flag to k8s commands.

For example:

kubectl create -f deployment.yaml --namespace=custom-namespace

you can list all the pods in custom-namespace.

kubectl get pods --namespace=custom-namespace

1
26

can also try kubectl delete all --all --all-namespaces

all refers to all resources

--all refers to all resources, including uninitialized ones

--all-namespaces in all all namespaces

WARNING: This will also delete kube-system pods, use with caution

5
  • 9
    That's like a nuclear bomb that would easily render your cluster useless as it would delete the kube-system namespace that Kubernetes itself requires for functioning.
    – donhector
    May 28, 2022 at 11:46
  • 5
    As destructive as it is, it does answer the original question best IMO. Jun 13, 2022 at 18:14
  • 1
    Doesn't include ingress. At least with eks
    – Souyama
    Oct 17, 2022 at 13:11
  • Both the post and the comments are useful. Maybe as a first step run kubectl get all --all-namespaces Mar 27, 2023 at 4:49
  • Actually, on v1.26.3 it doesn't delete kube-system Mar 27, 2023 at 4:51
25

You can also delete Kubernetes resources with the help of labels attached to it. For example, suppose below label is attached to all resource

metadata:
  name: label-demo
  labels:
    env: dev
    app: nginx

now just execute the below commands

deleting resources using app label
$ kubectl delete pods,rs,deploy,svc,cm,ing -l app=nginx

deleting resources using envirnoment label
$ kubectl delete pods,rs,deploy,svc,cm,ing -l env=dev
4
  • 12
    Note that you can also delete all resources. kubectl delete all -l env=dev
    – theduke
    Sep 17, 2020 at 10:47
  • 2
    It will only delete deployment, pods, rs, and svc.
    – Dan
    Nov 17, 2020 at 14:42
  • 1
    Although this command will work but it could be possible to miss kubernetes object having same label. So first it should be found which all objects are there with same label and then use this command. Apr 11, 2021 at 4:15
  • 1
    Yup true, it is always advice to add label to your resources.
    – Dan
    Jul 5, 2021 at 4:51
7

First backup your namespace resources and then delete all resources found with the get all command:

kubectl get all --namespace={your-namespace} -o yaml > {your-namespace}.yaml
kubectl delete -f {your-namespace}.yaml

Nevertheless, still some resources exists in your cluster. Check with

kubectl api-resources --verbs=list --namespaced -o name | xargs -n 1 kubectl get --show-kind --ignore-not-found --namespace {your-namespace}

If you really want to COMPLETELY delete your namespace, go ahead with:

kubectl delete namespace {your-namespace}

(tested with Client v1.23.1 and Server v1.22.3)

4
kubectl delete all -n <namespace> --all

--all argument should be the last argument

1
  • Shouldn’t this be a comment on the top-voted answer? Dec 15, 2023 at 0:21
3

In case if you want to delete all K8S resources in the cluster. Then, easiest way would be to delete the entire namespace.

kubectl delete ns <name-space>
1
1
kubectl delete deploy,service,job,statefulset,pdb,networkpolicy,prometheusrule,cm,secret,ds -n namespace -l label
1

kubectl delete all --all to delete all the resource in cluster. after deleting all resources k8's will again relaunch the default services for cluster.

Not the answer you're looking for? Browse other questions tagged or ask your own question.