48

Is there a different way than kubectl edit to delete an annotation in Kubernetes?

I do not like the interactivity of kubectl edit. I prefer something usable in a script.

2 Answers 2

113

Use minus - sign at the end of the annotation in kubectl annotate.

Example:

kubectl annotate service shopping-cart prometheus.io/scrape-

Removes annotation prometheus.io/scrape from shopping-cart service.

1
  • 1
    also works same way for istio virtual services kubectl annotate vs -n NAMESPACE VSERVICE kubectl.kubernetes.io/last-applied-configuration-
    – mati kepa
    Mar 24 at 13:48
8

An alternative way to achieve this goal, might be using kubectl patch command by invoking JSON patch method. Unfortunately it is more weighty solution comparing to your answer:

kubectl patch svc <Service_name> --type=json -p='[{"op": "remove", "path": "/metadata/annotations/prometheus.io~1scrape"}]'

More detailed information can be found in Jsonpatch document.

1
  • 2
    a regular kubectl patch setting the value of the annotation to null also works
    – jpbochi
    Feb 6, 2020 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.