kuberneteslabel

How to delete a label by kubernetes api?


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 ?


Solution

  • 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)