Delete Kubernetes Namespaces With kubectl
The short answer
In Kubernetes, to delete a namespace, you can use the [.inline-code]kubectl delete[.inline-code] command as follows:
Where:
- [.inline-code]namespace[.inline-code] is the name of the namespace you want to delete.
For example, the following command will delete the Kubernetes namespace named [.inline-code]dev[.inline-code]:
To retrieve the list of available namespaces in your cluster, you can read our other article on how to list namespaces in Kubernetes.
Note that deleting a namespace is an irreversible action and should be performed with caution.
[#delete-multiple-namespaces] Deleting multiple namespaces [#delete-multiple-namespaces]
To delete multiple namespaces at once, you can use the [.inline-code]kubectl delete[.inline-code] command as follows:
Where:
- [.inline-code]namespace …[.inline-code] is a list of namespaces separated by a space character.
For example, the following command will delete the [.inline-code]dev[.inline-code], [.inline-code]qa[.inline-code], and [.inline-code]staging[.inline-code] namespaces all at once:
[#delete-all-namespaces] Deleting all namespaces [#delete-all-namespaces]
To delete all the namespaces in a cluster at once, you can use the [.inline-code]kubectl delete[.inline-code] command with the [.inline-code]--all[.inline-code] flag as follows:
[#delete-namespace-by-label] Deleting namespaces by label [#delete-namespace-by-label]
In Kubernetes, labels are key-value pairs attached to the Kubernetes objects that organize resources based on specific criteria.
To delete one or more namespaces based on a label, you can use the [.inline-code]-l[.inline-code] flag (short for [.inline-code]--label[.inline-code]) as follows:
Where:
- [.inline-code]label[.inline-code] is the key of the label.
- [.inline-code]value[.inline-code] is the value associated with the label.
For example, the following command will remove all the namespaces with the label [.inline-code]env=dev[.inline-code]:
On the other hand, to delete all the namespaces except the ones with a specific label, you can place an exclamation mark [.inline-code]![.inline-code] before the label key as follows:
For example, the following command will delete all the namespaces in the current cluster except the namespace with the label [.inline-code]app=production[.inline-code]:
[#simulate-namespace-deletion] Simulating a namespace deletion [#simulate-namespace-deletion]
In Kubernetes, you can simulate the deletion of a namespace using the the [.inline-code]--dry-run[.inline-code] flag as follows:
Where:
- [.inline-code]strategy[.inline-code] can be one of [.inline-code]none[.inline-code], [.inline-code]client[.inline-code], or [.inline-code]server[.inline-code].
For example, the following command will simulate the deletion of the [.inline-code]dev[.inline-code] namespace on the client side:
[#client-vs-server-simulation] Client-side vs. server-side simulation [#client-vs-server-simulation]
When using the [.inline-code]--dry-run=client[.inline-code] flag, Kubernetes simulates the deletion operation on the client side, which means that [.inline-code]kubectl[.inline-code] locally validates the request but doesn't communicate with the server.
This is useful for quickly checking the syntax and feasibility of the deletion operation without impacting the cluster.
On the other hand, when using the [.inline-code]--dry-run=server[.inline-code] flag, Kubernetes simulates the deletion operation on the server side, which means that [.inline-code]kubectl[.inline-code] sends the deletion request to the Kubernetes API server, but the server does not actually execute the deletion.
Instead, it validates the request and returns the intended changes as if they were executed, allowing you to preview the server's response without affecting the cluster's actual state.
[#delete-namespaces-with-a-grace-period] Specifying a grace period [#delete-namespaces-with-a-grace-period]
In Kubernetes, the grace period refers to the amount of time given to a resource to perform any necessary cleanup or shutdown tasks before it is forcefully terminated.
To delete a namespace and all its related resources with a grace period expressed in seconds, you can use the [.inline-code]--grace-period[.inline-code] flag as follows:
Note that the grace period will default to 30 seconds if the [.inline-code]--grace-period[.inline-code] flag is omitted.
For example, the following command will remove the [.inline-code]dev[.inline-code] namespace and wait 90 seconds before forcefully terminating any related resources:
[#delete-namespaces-forcefully] Forcefully deleting a namespace [#delete-namespaces-forcefully]
To delete a namespace and its associated resources immediately without waiting for them to gracefully shutdown, you can use the [.inline-code]--force[.inline-code] flag as follows:
Note that the [.inline-code]--force[.inline-code] flag is mandatory when setting the grace period to [.inline-code]0[.inline-code]:
[#easily-recall-syntax-with-ai] Easily retrieve this command using Warp’s AI Command Search [#easily-recall-syntax-with-ai]
If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Search feature:
Entering [.inline-code]kubernetes delete namespace with delay[.inline-code] in the AI Command Suggestions will prompt a list of [.inline-code]kubectl[.inline-code] commands that can then quickly be inserted into your shell by doing [.inline-code]CMD+ENTER[.inline-code].
[#remove-namespace-finalizers] Removing the finalizers attached to a namespace [#remove-namespace-finalizers]
When trying to delete a namespace, it may happen that some of its related resources get stuck in a "Terminating" state, thus preventing its deletion.
To effectively remove any finalizers attached to the specified namespace, allowing it to be deleted without any final cleanup or processing actions taking place, you can use the [.inline-code]kubectl patch[.inline-code] command as follows:
Note that this command should be used with caution, as finalizers are important for the orderly deletion of resources.
[#common-errors] Common errors [#common-errors]
Here are some of the most common errors when deleting a namespace in Kubernetes.
[#permission-denied] Permission denied [#permission-denied]
The [.inline-code]kubectl[.inline-code] command will output an error when trying to delete a namespace with a Kubernetes user lacking the necessary roles and permissions.
For example, trying to remove the default namespace will result in the following error:
[#namespace-not-found] Namespace not found [#namespace-not-found]
The [.inline-code]kubectl[.inline-code] command will output an error if the specified namespace doesn't exist.
For example, trying to remove the non-existent [.inline-code]test[.inline-code] namespace will result in the following error: