11

To setup the kubernetes, I started with creating namespace, deployment, service. To clean the resources, do I need to follow any order like remove the service first then pods and then deployment and finally namespace? how to clean the resources in a proper way? Because I deleted pods and service, but I could see the pods,services running again. Its deploying the resources again, so this question came up here for experts answers.

5 Answers 5

24

Just in case you are running them in default namespace and there are many of them ,and you don't want to spend time on deleting them one by one:

kubectl delete deployments --all
kubectl delete services --all
kubectl delete pods --all
kubectl delete daemonset --all
15

I deleted the resources in the below order and it worked for me.

To get all the resources.

kubectl get pods,services,deployments,jobs,daemonset

Delete the resources like below:

kubectl delete deployments <deployment>
kubectl delete services <services>
kubectl delete pods <pods>
kubectl delete daemonset <daemonset>
8

I follow a different method to clean up resources allocated for an application.

$ kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-75b7bfdb6b-k76tl   1/1     Running   0          9d


$ kubectl get deployments
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
nginx   1/1     1            1           9d

So now instead of deleting the objects/resources individually for an application, use delete all which removes all the resources associated with the application.

This way Kubernetes cleanup the resources rather than we decide the order of cleanup.

$ kubectl delete all -l app=nginx
pod "nginx-75b7bfdb6b-k76tl" deleted
service "nginx-service" deleted
deployment.apps "nginx" deleted
replicaset.apps "nginx-75b7bfdb6b" deleted

Hope this helps!

2
  • Actually it works! I had a tunnel running which was the reason to respin the pods. Commented Aug 6, 2020 at 0:01
  • 1
    kubectl explain [resource] and kubectl get [resource] -o yaml can help people find the -l, --selector=' that would help them clean up their configs.
    – nelaaro
    Commented Feb 10, 2021 at 18:14
7

If you just delete the namespace, it will delete everything in it, you don’t need to manually clean up everything in it. I did a web search for “Kubernetes delete namespace” and found this in the official Kubernetes docs: https://kubernetes.io/docs/tasks/administer-cluster/namespaces/#deleting-a-namespace

Regarding your question about pods coming back: when you create a deployment, it creates pods. When you delete the deployment, it will automatically delete pods it created. If you manually delete the pods that the deployment automatically created, it will bring them back because the desired number of replicas as specified in your deployment is still a positive number.

If you create pods directly (not via a deployment), you can delete them directly, and they will stay deleted.

Pods (that were created directly), deployments, and services can all be deleted independently of one another, order doesn’t matter. If you want to delete them but not the namespace, delete them in any order. If you want to delete the namespace as well, just delete the namespace and it will automatically also delete everything in the namespace for you, you don’t need to bother also individually deleting those things.

4

First find out what you, or things that have added into the mix (see sidecar injection).

For any given installation of k8s there are different objects that may show up. Most of the namespaced objects can be listed via the shell fragment below. ( just set the variable NAMESPACE to the namespace in question.

kubectl get --namespace $NAMESPACE $(kubectl api-resources --namespaced=true  --no-headers=true  | awk  ' { print $1 } ' | perl -e '@foo = map { s/\n//; $_; }  <> ;  print join "," , @foo ; ' ) 

There will probably be some events, and a default account or secret or two.

Now that you know of all the things, you can delete all the things. Please do it carefully and include both --namespace and --context flags for each use of kubectl.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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