A developer deleting a Kubernetes namespace.
Image: BalanceFormCreative/Adobe Stock

Kubernetes namespaces are a means to divide resources between users and teams so there’s no cross-pollination or confusion about what belongs to what and which deployments go where. Namespaces are a great way to spread those resources across your teams and projects.

SEE: Hiring kit: Back-end Developer (TechRepublic Premium)

As you and your teams work with namespaces, you will eventually run into a situation where one of those namespaces is no longer needed, but for whatever reason, cannot be deleted. That can be problematic when you need to use that same namespace for a project or when the stubborn namespace causes problems for other projects.

Fortunately, there’s a way to get rid of those pesky namespaces. It’s not easy, but it’s repeatable and reliable. Let me show you how.

What you’ll need to delete a stubborn Kubernetes namespace

The only things you’ll need for this process are a working Kubernetes cluster, a stuck namespace and a user that can run the kubectl command.

How to edit the namespace configuration

Let’s imagine our problematic namespace is called projectx. The first thing we need to do is edit the namespace configuration file for projectx.

SEE: The Complete DevOps Certifications Courses & Practice Tests Bundle (TechRepublic Academy)

Start by opening a terminal window on the Kubernetes controller and dumping the contents of that namespace configuration into a file with the command:

kubectl get ns projectx -o json > tmp.json

Next, we need to edit that file with the command:

nano tmp.json

In that file, you’ll find a block of code that includes a section like this:

"spec": {

    "finalizers": [

It might also look like this:

}, "spec": { "finalizers": [ "kubernetes" ] },

From that section, you need to remove the “kubernetes” entry, so it looks like this:

"spec": {

    "finalizers": [

Or like this:

}, "spec": { "finalizers": [ ] },

You should now save and close the file. Leave this terminal open.

How to apply the new configuration file

We need to apply the altered configuration file. For this, open a second terminal window on the Kubernetes controller and run the proxy command:

kubectl proxy

Go back to the original terminal and apply the edited config file with the command:

curl -k -H "Content-Type: application/json" -X PUT --data-binary @tmp.json

Applying the configuration will delete the namespace. Once the configuration file has been applied, you can verify the namespace has been deleted with this command:

kubectl get ns projectx

The output should look something like this:

Error from server (NotFound): namespaces "projectx" not found

Namespace deletion and other tutorials

That’s it: You’ve successfully deleted that stubborn namespace. This is a task you should get used to doing, because namespaces can become problematic, and you’ll need to be able to get rid of them when they arise. Now you know how.

SEE: The Ultimate Kubernetes & Cloud Certification Training Bundle (TechRepublic Academy)

Our TechRepublic tutorial library covers a variety of issues with Kubernetes and other technologies that you may face. To learn more about how to get the most out of your developer projects, take a look at our developer tutorials library here.

Read next: Best project management software (TechRepublic)