Skip to main content
When managing Kubernetes clusters, you may need to delete resources such as pods, deployments, or namespaces. In many cases, you can remove a pod using a simple command:
For example:
This command typically works as expected. However, sometimes a resource may not terminate correctly. In this article, we will explore the reasons behind such issues and provide methods to resolve them.

Example: Pod Stuck in Termination

Consider a pod named shipping-api-57cdd984bc-grq7g. Deleting it might initially return:
Yet, when you inspect the pod list:
Some pods remain in the Terminating state. This behavior is often due to background operations or cleanup tasks (similar to garbage collection) that must complete before the resource is fully removed.

Using the —force Flag

One approach to handle this issue is to force delete the resource using the --force flag. Note that force deletion does not wait for confirmation that the underlying resource has been terminated:
After executing the forced deletion, if you check for remaining pods, there is a possibility that the resource may still be present or continue running if the underlying dependencies have not been cleaned up.
Using the --force flag can lead to unintended side effects. Use this option sparingly and only when necessary.

Removing Finalizers

Another effective method involves removing finalizers manually. Finalizers ensure that specific cleanup tasks—such as persistent volume or namespace protection actions—are completed before the resource is deleted. When editing a pod stuck in termination, you might notice a finalizer in its configuration. For example:
To allow the pod to be fully deleted, remove or set the finalizers to null and save the changes. This method is not just limited to pods; it can also be applied to other Kubernetes resources such as PersistentVolumeClaims (PVCs) and namespaces.

Handling Stuck Namespaces

Sometimes, the issue isn’t limited to pods. Entire namespaces may get stuck in the terminating state. For example, you might have a namespace called stable that does not delete as expected. You could try force deletion:
If the namespace remains stuck, remove the finalizers from its manifest. An edited namespace manifest might appear as follows:
After removing (or nullifying) the finalizers and saving your changes, re-check the namespaces. The stable namespace should be successfully deleted once Kubernetes completes the finalization step.

Verifying Resource Deletion

After using either forced deletion or manually removing finalizers, it is important to verify that the resource is no longer present. For example, check the status of pods with:
Similarly, verify namespace deletion:
Always investigate further if resources remain stuck in the terminating state. Relying solely on forced deletion or removal of finalizers can mask underlying issues that require a deeper investigation.

Final Thoughts

Managing resources in Kubernetes can sometimes lead to challenges such as lingering terminating pods or namespaces. By understanding the role of finalizers and the implications of force deletion, you can better troubleshoot and resolve these issues. For more information on Kubernetes resource management, consider exploring the following resources: Happy troubleshooting!

Watch Video