0

Being new to the K8s, I am trying to clean up the whole namespace after running some tests on a Windows 10 machine. In short, I thought it would be as easy as running kubectl.exe delete deployment but the deployments are created back after a second and I don't know how to get rid of them. See the followings for the details of what I did:

1.kubectl get deployments,rs (to see what we already have)

NAME                               DESIRED   CURRENT   UP-TO-DATE   AVAILABLE AGE
deployment.extensions/postgresql   1         1         1            1           18m
deployment.extensions/redis        1         1         1            1           16m
NAME                                         DESIRED   CURRENT   READY     AGE  
replicaset.extensions/postgresql-c8cb9fff6   1         1         1         18m
replicaset.extensions/redis-5678477b7c       1         1         1         16m

2. kubectl scale deployment redis --replicas=0 (Scale down the deployment)

deployment.extensions "redis" scaled

3. kubectl get deployments,rs (Check again how it looks)

NAME                               DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
deployment.extensions/postgresql   1         1         1            1           21m
deployment.extensions/redis        0         0         0            0           19m
NAME                                         DESIRED   CURRENT   READY     AGE
replicaset.extensions/postgresql-c8cb9fff6   1         1         1         21m
replicaset.extensions/redis-5678477b7c       0         0         0         19m

4. kubectl delete deployment.extensions/redis (Delete the deployment)

deployment.extensions "redis" deleted

5. kubectl get deployments,rs (Check again and see that it is back!)

NAME                               DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
deployment.extensions/postgresql   1         1         1            1           23m
deployment.extensions/redis        1         1         1            1           27s  
NAME                                         DESIRED   CURRENT   READY     AGE
replicaset.extensions/postgresql-c8cb9fff6   1         1         1         23m
replicaset.extensions/redis-5678477b7c       1         1         1         27s

6. kubectl.exe get events (Looking into the events):
Among other things I can see "Scaled down replica set redis-5678477b7c to 0" and then "Scaled up replica set redis-5678477b7c to 1" which looks like it was never actually deleted but immediately scaled up again after the delete command was executed.

Not sure what I am missing but have already checked a couple of other posts like Kubernetes pod gets recreated when deleted and How to delete all resources from Kubernetes one time? but neither one worked for me.

Forgot to say that the K8s cluster is managed by the Docker Desktop.

Ramin Toussi
  • 97
  • 1
  • 11

1 Answers1

1
  1. Use kubectl delete deployment <the name of deployment >

  2. If you need to clean up whole namespace , use kubectl delete namespace <namespace-name>

  3. Then re-create the same namespace by kubectl create ns command , if you need the same namespace.

  4. You can also clean up the namespace by using --all options with objects:

e.g

   kubecetl delete deployment --all
   kubecetl delete statefulset --all
   kubectl delete pvc --all
   kubectl delete secrets --all
   kubectl delete service --all

and so on.

As pointed out by @David Maze, you're deleting the ReplicaSet instead of the Deployment that's managing it.

From the documentation:

You can define Deployments to create new ReplicaSets

The Deployment will automatically create and manage a ReplicaSet to control the pods. You need to delete the Deployment to erase it's managed resources.

Ijaz Ahmad
  • 11,198
  • 9
  • 53
  • 73
  • Those objects belong to the "default" namespace which is forbidden and may not be deleted :( Do you know any way to delete the default ns? – Ramin Toussi Jul 09 '19 at 11:08
  • It might be best if you delete the deployment instead of the namespace. – yyyyahir Jul 09 '19 at 11:17
  • @RaminToussi 4th option added for that in the answer. – Ijaz Ahmad Jul 09 '19 at 11:59
  • it is recommended not to create stuff in default namespace , create custom namespace for everything so that its easyt o clean up later, plus other reasons as well – Ijaz Ahmad Jul 09 '19 at 12:03
  • @IjazAhmadKhan as I corrected in my post, even deleting the deployment did not help. But I agree with you other comment about creating a separate namespace, that is a good practice to always follow. – Ramin Toussi Jul 09 '19 at 12:20
  • @RaminToussi ok i think it maybe a statefulset , please paste the output of all the obejcts that you have created in default namespace so that i know what u need to delete , answer update for delete statefulset – Ijaz Ahmad Jul 09 '19 at 12:30
  • `kubecetl delete statefulset --all` – Ijaz Ahmad Jul 09 '19 at 12:31
  • if you have deployed things using helm , you need to do `helm delete --purge ` – Ijaz Ahmad Jul 09 '19 at 12:34