<p>I'm trying to find the API equivalent of <a href="https://stackoverflow.com/q/54973593/5291015">How to remove (delete) annotation in Kubernetes
</a> to remove annotation for a service type in Python. Doing it from the command line works perfectly fine.</p>
<p>The latest <a href="https://github.com/kubernetes-client/python/blob/master/kubernetes/README.md" rel="nofollow noreferrer">kubernetes-client/python</a> does not have any APIs that allow to patch annotations. I could always delete and recreate the service, but I'd like to patch it.</p>
<p>This is a simple MCVE if someone wants. A test service YAML</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Service
metadata:
  name: my-service
  annotations:
    description: my test service
spec:
  ports:
    - protocol: TCP
      port: 80
      targetPort: 9376
</code></pre>
<p>and the Python code I'm using</p>
<pre class="lang-py prettyprint-override"><code>from kubernetes import client, config
from kubernetes.client.rest import ApiException

config.load_kube_config()

coreV1 = client.CoreV1Api()
appsV1 = client.AppsV1Api()

try:
    resp = coreV1.read_namespaced_service(&quot;my-service&quot;, &quot;default&quot;)
    del resp.metadata.annotations[&quot;description&quot;]
    patch = coreV1.patch_namespaced_service(&quot;my-service&quot;, &quot;default&quot;, body=resp)
    print(patch)

except ApiException as e:
     print(str(e))
</code></pre>

<p>An annotation is part of the overall metadata for a resource. The API functions are related to patching the resource, not the annotation specifically.</p>
<p>The Kubernetes API documents a <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.19/#patch-service-v1-core" rel="nofollow noreferrer">PATCH to a Service</a> which equates to <code>patch_namespaced_service</code> in the <a href="https://github.com/kubernetes-client/python/blob/master/kubernetes/README.md" rel="nofollow noreferrer">python API</a>.
The JSON required to delete an annotation is:</p>
<pre><code>{
  &quot;metadata&quot;: {
    &quot;annotations&quot;: {
      &quot;your/thing&quot;: null
    }
  }
}
</code></pre>
<p>Python will serialise <code>None</code> to <code>null</code></p>
<pre class="lang-py prettyprint-override"><code>coreV1.patch_namespaced_service(&quot;my-service&quot;, &quot;default&quot;, body={
  &quot;metadata&quot;:{&quot;annotations&quot;:{&quot;description&quot;: None}}
})
</code></pre>
<p>For any operation, the cli can output the REST calls and &quot;Request Body&quot; if you increase the log level verbosity with <code>kubectl -v8</code>:</p>
<pre><code>I1112 05:34:08.910667   10552 request.go:1097] Request Body: {&quot;metadata&quot;:{&quot;annotations&quot;:{&quot;your/thing&quot;:null}}}
I1112 05:34:08.910771   10552 round_trippers.go:420] PATCH https://k8s:6443/api/v1/namespaces/default/services/kubernetes?fieldManager=kubectl-annotate
</code></pre>
