How to delete all the contents from a kubernetes node?
Contents include deployments, replica sets etc. I tried to delete deplyoments seperately. But kubernetes recreates all the pods again.
Is there there any ways to delete all the replica sets present in a node?
4 Answers
If you are testing things, the easiest way would be
kubectl delete deployment --all
Althougth if you are using minikube, the easiest would probably be delete the machine and start again with a fresh node
minikube delete
minikube start
If we are talking about a production cluster, Kubernetes has a built-in feature to drain a node of the cluster, removing all the objects from that node safely.
You can use
kubectl drain
to safely evict all of your pods from a node before you perform maintenance on the node. Safe evictions allow the pod’s containers to gracefully terminate and will respect the PodDisruptionBudgets you have specified.Note: By default
kubectl drain
will ignore certain system pods on the node that cannot be killed; see the kubectl drain documentation for more details.When
kubectl drain
returns successfully, that indicates that all of the pods (except the ones excluded as described in the previous paragraph) have been safely evicted (respecting the desired graceful termination period, and without violating any application-level disruption SLOs). It is then safe to bring down the node by powering down its physical machine or, if running on a cloud platform, deleting its virtual machine.
First, identify the name of the node you wish to drain. You can list all of the nodes in your cluster with
kubectl get nodes
Next, tell Kubernetes to drain the node:
kubectl drain <node name>
Once it returns (without giving an error), you can power down the node (or equivalently, if on a cloud platform, delete the virtual machine backing the node). drain
waits for graceful termination. You should not operate on the machine until the command completes.
If you leave the node in the cluster during the maintenance operation, you need to run
kubectl uncordon <node name>
afterwards to tell Kubernetes that it can resume scheduling new pods onto the node.
Please, note that if there are any pods that are not managed by ReplicationController, ReplicaSet, DaemonSet, StatefulSet or Job, then drain
will not delete any pods unless you use --force, as mentioned in the docs.
kubectl drain <node name> --force

- 12,794
- 8
- 51
- 56
-
Hi, actually Kuberntes restarted all the old pods when i applied kubectl uncordon command. That means the pods were not deleted. They just stopped execution. I wanted every contents be deleted permanently from the node. Any help? – Mufeed Jan 04 '18 at 06:31
-
I guess you have a one node cluster? The kubernetes scheduler will schedule (that means "start") pods on available nodes that are part of the cluster, until reach the desired amount of replicas for your deployments. You can't remove everything from a node if it's available to schedule new objects, that's why we use cordon/drain. So if you only have one node, the scheduler will re-create the objects until reaching the desired number of replicas. Why do you want to delete everything from a node? – Jose Armesto Jan 04 '18 at 12:10
-
Yes it was one node cluster. I used Minikube. I created many deployments for learning purpose. Lately maintaining these deployments was really hard to me. So I decided to clean up everything. – Mufeed Jan 05 '18 at 09:39
-
Can you show me how did you create these deployments? Normally, `kubectl delete deployment --all` should do what you want then. – Jose Armesto Jan 05 '18 at 11:24
-
I used yaml files to create deployments. Then ran kubectl create command. – Mufeed Jan 05 '18 at 11:33
-
If you are using minikube, you can even delete your machine and start again using `minikube delete` and then `minikube start`. – Jose Armesto Jan 05 '18 at 12:06
-
oops..sorry. I dint check it yet. I had some other works to do lately. I will let you know once I check this command. – Mufeed Jan 08 '18 at 14:02
minikube delete --all
in case you are using minikube
it will let you start a new clean cluster.
in case you run on Kubernetes :
kubectl delete pods,deployments -A --all
it will remove it from all namespaces, you can add more objects in the same command .

- 715
- 6
- 5
Kubenertes provides namespaces object for isolation and separation of concern. Therefore, It is recommended to apply all of the k8s resources objects (Deployment, ReplicaSet, Pods, Services and other) in a custom namespace.
Now If you want to remove all of the relevant and related k8s resources, you just need to delete the namespace which will remove all of these resources.
kubectl create namespace custom-namespace
kubectl create -f deployment.yaml --namespace=custom-namespace
kubectl delete namespaces custom-namespace
I have attached a link for further research.

- 17,341
- 8
- 47
- 55
-
-
Hi Mufeed, As I understand, If I create a namespace " my-ns" and then I apply deployment and service in my-ns namespace. If I delete my-ns namespace eventually all of the resources would be deleted. It always works. If you can show the resources in your namespace `kubectl get all -n my-ns` . we can resolve it – Suresh Vishnoi Jan 04 '18 at 10:22
I tried so many variations to delete old pods from tutorials, including everything here.
What finally worked for me was:
kubectl delete replicaset --all
Deleting them one at a time didn't seem to work; it was only with the --all
flag that all pods were deleted without being recreated.

- 1,224
- 12
- 16