How to Delete Namespace in Kubernetes: Simple Commands & Tips
To delete a namespace in Kubernetes, use the
kubectl delete namespace <namespace-name> command. This removes the namespace and all resources inside it, but the deletion may take some time to complete.Syntax
The basic syntax to delete a namespace in Kubernetes is:
kubectl delete namespace <namespace-name>: Deletes the specified namespace and all its resources.
Here, <namespace-name> is the name of the namespace you want to remove.
bash
kubectl delete namespace <namespace-name>
Example
This example shows how to delete a namespace named test-namespace. The command removes the namespace and all resources inside it.
bash
kubectl delete namespace test-namespace
Output
namespace "test-namespace" deleted
Common Pitfalls
Common mistakes when deleting namespaces include:
- Trying to delete the
defaultorkube-systemnamespaces, which are critical for cluster operation. - Not waiting for the namespace deletion to complete, as it can take some time if many resources exist.
- Deleting a namespace without checking what resources it contains, which can cause unexpected data loss.
Always verify the namespace contents before deletion using kubectl get all -n <namespace-name>.
bash
kubectl get all -n test-namespace kubectl delete namespace test-namespace
Output
NAME READY STATUS RESTARTS AGE
pod/example-pod 1/1 Running 0 2d
namespace "test-namespace" deleted
Quick Reference
| Command | Description |
|---|---|
| kubectl delete namespace | Deletes the specified namespace and all its resources |
| kubectl get namespaces | Lists all namespaces in the cluster |
| kubectl get all -n | Lists all resources inside a specific namespace |
| kubectl describe namespace | Shows detailed info about a namespace |
Key Takeaways
Use 'kubectl delete namespace ' to remove a namespace and its resources.
Namespace deletion can take time; wait until it fully completes.
Avoid deleting system namespaces like 'default' or 'kube-system'.
Check namespace contents before deletion to prevent data loss.
Use 'kubectl get all -n ' to list resources inside a namespace.