Skip to content
LatchkeyLatchkey home

kubectl drain --force: Usage, Options & Common CI Errors

Drain a node even when it runs unmanaged or local-storage pods.

kubectl drain --force evicts a node's pods including bare pods not backed by a controller, which a plain drain refuses to touch. With its companion flags it is how node-lifecycle automation empties a node before maintenance.

What it does

kubectl drain NODE cordons the node and evicts its pods via the eviction API, honouring PodDisruptionBudgets. --force additionally deletes unmanaged ("naked") pods that would otherwise block the drain. --ignore-daemonsets skips DaemonSet pods, and --delete-emptydir-data permits evicting pods with emptyDir local data (which is lost).

Common usage

Terminal
kubectl drain ip-10-0-1-5 --ignore-daemonsets --delete-emptydir-data
kubectl drain ip-10-0-1-5 --force --ignore-daemonsets
kubectl drain ip-10-0-1-5 --grace-period=120 --timeout=5m --ignore-daemonsets
# ... maintenance ...
kubectl uncordon ip-10-0-1-5

Common errors in CI

"cannot delete Pods not managed by ReplicationController, ReplicaSet, Job, DaemonSet or StatefulSet" needs --force - but understand --force here means those bare pods are deleted and not rescheduled anywhere, so they are simply gone. "cannot delete DaemonSet-managed Pods" needs --ignore-daemonsets; "cannot delete Pods with local storage" needs --delete-emptydir-data (the data is destroyed). The blocker that hangs automation is a PodDisruptionBudget: "Cannot evict pod ... violates the disruption budget" makes drain retry until another replica is Ready - set --timeout so the step fails instead of hanging.

Using this in CI

A runner has no kubeconfig, no cached context, and no interactive auth. Every kubectl invocation in CI needs the context supplied explicitly, and most confusing CI failures here are the command running against the wrong cluster or no cluster at all.

Terminal
# never rely on the ambient context on a runner
kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" get pods

# confirm what you are actually connected to before mutating anything
kubectl config current-context
kubectl cluster-info

# fail fast instead of hanging on an unreachable API server
kubectl --request-timeout=30s get nodes

Frequently asked questions

kubectl drain --force: Usage, Options & Common CI Errors?
kubectl drain --force evicts a node's pods including bare pods not backed by a controller, which a plain drain refuses to touch. With its companion flags it is how node-lifecycle automation empties a node before maintenance.
What it does?
kubectl drain NODE cordons the node and evicts its pods via the eviction API, honouring PodDisruptionBudgets. --force additionally deletes unmanaged ("naked") pods that would otherwise block the drain. --ignore-daemonsets skips DaemonSet pods, and --delete-emptydir-data permits evicting pods with emptyDir local data (which is lost).
Common errors in CI?
"cannot delete Pods not managed by ReplicationController, ReplicaSet, Job, DaemonSet or StatefulSet" needs --force - but understand --force here means those bare pods are deleted and not rescheduled anywhere, so they are simply gone.

Related guides

References

Run this faster and cheaper on Latchkey managed runners - self-healing included. Start free → 30-day trial · No credit card