Search code examples
kubernetes

How to delete all resources from Kubernetes one time?


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?


Solution

  • 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