Kubernetes is a powerful tool for managing containerized applications, but sometimes resources can become stuck and difficult to remove. This can happen for a variety of reasons, such as conflicts with other resources, problems with the resource itself, or issues with the Kubernetes cluster.

Here are some things you can try to delete a resource…

The regular way

Delete a single resource:

kubectl delete RESOURCETYPE RESOURCENAME -n NAMESPACE

While -n NAMESPACE can be omited if the resource is in the default namespace or if resources of this resource type can’t be in namespaces. Like Cluster Roles, Storage classes or Namespaces.

Delete all resources in a namespace:

kubectl delete -n wordpress --all

Now you have to wait for a while. But if these commands do not work there might be a problem.You can still try to force the deletion…

Edit the YAML to remove finalizers

Finalizers tell Kubernetes what to do before a resource can be deleted. Sometimes things get stuck here. So one thing you can try is removing the finalizers from the stuck resources by hand.

You can edit the YAML of the stuck resource like this:

kubectl edit RESOURCETYPE RESOURCENAME -n NAMESPACE

This will open your editor with a YAML file.

Search for the key “finalizers:

spec:
  finalizers:
    -xxxxxxxxxxxxxxx
  blablabla:
    asodijoi

Delete the line “finalizers:” and the lines that belong to it. Don’t delete the lines that follow. In this example “blablabla” must remain but “-xxxxxxxxxxxxxxx” must be deleted.

Force delete Resource

You can try the force deletion with –force –grace-period=0.

kubectl delete RESOURCETYPE RESOURCENAME -n NAMESPACE --force --grace-period=0 

Delete a stucking namespace

If the resource is of type Namespace then you can check if its actually empty:

kubectl get -n NAMESPACENAME all

This should show nothing. Otherwise You might want to try to delete those resources first:

kubectl delete -n NAMESPACENAME --all


Could I help? Buy me a drink ! 💙