Pods, Deployments, Services and any other Kubernetes (K8s) resources can be deleted simply by using the kubectl delete command.
However, you may want to force the deletion, especially if some Pod or Deployment got stuck in a “Terminating” or “Unknown” state.
This post shows how to forcefully delete K8s resources using a kubectl command.
Cool Tip: Force delete a “Terminating” Namespace using kubectl! Read more →
Delete K8s Resources Forcefully using Kubectl
⚠️ Warning: A force deletion of some Kubernetes resources may result in inconsistency or data loss and requires confirmation.
To delete any Kubernetes resource (Pod, Deployment, Service, etc.) forcefully, execute the kubectl delete command with the --grace-period=0 and --force flags, as shown below:
$ kubectl delete <resourceType> <resourceName> --grace-period=0 --force
- examples -
$ kubectl delete pod foo --grace-period=0 --force
$ kubectl delete deployment baz --grace-period=0 --force
$ kubectl delete service qux --grace-period=0 --force
The --force option will delete the resource immediately and the --grace-period=0 option will terminate it without waiting for the grace period.
| Option | Description |
|---|---|
--force |
Immediately remove resources from API and bypass graceful deletion. |
--grace-period=0 |
Period of time in seconds given to the resource to terminate gracefully. |
If, after deletion attempt, a resource, e.g. some Pod or Deployment, got stuck in a “Terminating” or “Unknown” state, execute the following command to remove any finalizers, that should force the deletion:
$ kubectl patch <resourceType> <resourceName> -p '{"metadata":{"finalizers":null}}'
- examples -
$ kubectl patch pod foo -p '{"metadata":{"finalizers":null}}'
$ kubectl patch deployment baz -p '{"metadata":{"finalizers":null}}'
$ kubectl patch service qux -p '{"metadata":{"finalizers":null}}'
ℹ️ Finalizers are namespaced keys that tell Kubernetes to wait until specific conditions are met before it fully deletes resources marked for deletion.
Cool Tip: A wildcard deletion of Pods using a kubectl command! Read more →