Usually, we delete a label like that 'kubectl label namespace namespace_name labelname-'
But I want to delete it by kubernetes api, what should I do ?
Kubectl is internally calling REST API exposed by Kubernetes API Server to perform any operation including label deletion. But if you want to do it programmatically then you can use kubernetes client libraries of different languages.
You can also curl the Kubernetes REST API yourself.
https://kubernetes.io/docs/tasks/access-application-cluster/access-cluster/
As far as I'm aware, you won't be able to delete the label but replace/rename it.
That might be doe using patch
option which you can read about on Update API Objects in Place Using kubectl patch.
There is also several questions on Stack Overflow regarding this subject. - How to delete a label for a kubernetes pod - Kubernetes API: How to add/remove label from Node - How to remove a node label with kubernetes API
Also here is an example how using a Kubernetes Python Client you could patch the label:
from pprint import pprint
from kubernetes import client, config
config.load_kube_config()
client.configuration.debug = True
api_instance = client.CoreV1Api()
body = {
"metadata": {
"labels": {
"label-name": None}
}
}
api_response = api_instance.patch_node("minikube", body)
print(api_response)