How to delete a Kubernetes namespace

Kubernetes namespaces group namespaced objects under one API scope, so deleting one is a cluster cleanup action rather than only a workload delete. It is useful when a test environment, tenant space, or temporary application area has reached the end of its life and should no longer accept resources.

Namespace deletion marks the namespace for termination and lets Kubernetes remove namespaced objects such as Pods, Deployments, Services, ConfigMaps, Secrets, Roles, and RoleBindings. Cluster-scoped objects are outside the namespace, and storage or cloud-provider resources may depend on the controllers that created them.

Run the delete from the intended kubectl context and inspect the namespace before removal. The cleanup is finished when the wait command completes and a final lookup returns NotFound for the namespace name.

Steps to delete a Kubernetes namespace:

  1. Check the current kubectl context.
    $ kubectl config current-context
    platform-admin
  2. List the resources in the namespace.
    $ kubectl get all,configmap --namespace team-a
    NAME                       READY   STATUS              RESTARTS   AGE
    pod/web-7d876db474-m777g   0/1     ContainerCreating   0          0s
    
    NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
    deployment.apps/web   0/1     1            0           0s
    
    NAME                             DESIRED   CURRENT   READY   AGE
    replicaset.apps/web-7d876db474   1         1         0       0s
    
    NAME                         DATA   AGE
    configmap/app-settings       1      0s
    configmap/kube-root-ca.crt   1      0s
  3. Delete the namespace without waiting inside the delete command.
    $ kubectl delete namespace team-a --wait=false
    namespace "team-a" deleted

    Deleting a namespace removes namespaced resources under it. Confirm the name before running the command in a shared or production cluster.

  4. Wait for Kubernetes to remove the namespace object.
    $ kubectl wait --for=delete namespace/team-a --timeout=120s
    namespace/team-a condition met

    If the wait times out, inspect the namespace conditions and remaining objects before changing finalizers.

    $ kubectl describe namespace team-a


    Related: How to check Kubernetes events

  5. Verify that the namespace is absent.
    $ kubectl get namespace team-a
    Error from server (NotFound): namespaces "team-a" not found