Skip to content
Latchkey

kubectl label --overwrite: Usage, Options & Common CI Errors

Change a label that already exists without the re-run failure.

kubectl label --overwrite updates the value of a label that is already set, which the bare label command refuses to do. It is the flag that makes labelling idempotent in a re-runnable CI pipeline.

What it does

kubectl label RESOURCE NAME key=value --overwrite replaces an existing label's value (without it, re-labelling an existing key errors). It composes with -l to bulk-relabel by selector and --all to cover a namespace; key- still removes a label regardless of --overwrite.

Common usage

Terminal
kubectl label pod my-pod env=prod --overwrite
kubectl label pods -l app=web tier=frontend --overwrite
kubectl label deploy/web version=${GIT_SHA} --overwrite
kubectl label node ip-10-0-1-5 disktype=ssd --overwrite

Common errors in CI

"\"env\" already has a value (staging), and --overwrite is false" is the canonical re-run trap: the first run set the label, the retry fails because --overwrite was omitted - add it for idempotent scripts. "metadata.labels: Invalid value" means the value breaks the rules: ≤63 characters, alphanumeric plus -, _, ., starting and ending alphanumeric. Git branch names and image tags routinely violate this (slashes, leading non-alphanumeric), so sanitize before labelling. Relabelling a pod that a Service selects on can move it out of (or into) the Service's endpoints, so mind selector-driven side effects.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →