How to delete a label for a kubernetes pod

5/27/2016

I want delete label from a node or a pod by kubernetes API, my kubernetes version:1.24

kubectl get pod --show-labels | grep all-flow-0fbah
all-flow-0fbah      1/1     Running   2        9d      app=all-flow,op=vps1,version=1001

I use command as below:

 curl --request PATCH --header "Content-Type:application/json-patch+json" http://10.93.78.11:8080/api/v1/namespaces/default/pods/all-flow-0fbah --data '{"metadata":{"labels":{"a":"b"}}}'

But this doesn't work. Return message as below:

{
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {},
  "status": "Failure",
  "message": "the server responded with the status code 415 but did not return more information",
  "details": {},
  "code": 415
}

Then I change the curl header like this :

curl --request PATCH --header "Content-Type:application/merge-patch+json" http://10.93.78.11:8080/api/v1/namespaces/default/pods/all-flow-0fbah --data '{"meadata":{"labels":{"op":"vps3"}}}'

It not delete label but add a new label to that pod. So is there any one can tell me how to delete a label for a pod like use command :

kubectl label pod all-flow-0fbah key-

Thanks!

-- workhardcc
docker
kubernetes

5 Answers

10/3/2017

Was looking for CLI command myself. Here is what worked for me:

kubectl patch pod <podname> --type=json -p='[{"op": "remove", "path": "/metadata/labels/somelabelkey"}]'
-- user2174835
Source: StackOverflow

6/3/2016

The most convenient way is to use kubectl edit pods all-flow-0fbah. Just remove the label field in the editor.

See http://kubernetes.io/docs/user-guide/kubectl/kubectl_edit/

-- Lantao Liu
Source: StackOverflow

10/25/2019

I think this is the straightforward way to do that, which I found easier:

kubectl label pod <pod-name> <label key>-
-- naresh_nayini
Source: StackOverflow

12/19/2016

As workhardcc posted in a comment, the way to do this is

curl -k -v -XPATCH -H "Accept: application/json, /" -H "Content-Type: application/strategic-merge-patch+json" 10.10.10.10:443/api/v1/namespaces/default/pds/all-flow-946y0 --data '{"metadata":{"labels":{"$patch": "delete", "app":"all-flow"}}}'
-- Robert Bailey
Source: StackOverflow

10/9/2018

In additions to answers above, it's worth mentioning that if you use JSON Patch to remove remove a label/annotation with a slash (/) or a tilde (~) in it you'll have to escape them as mentioned in http://jsonpatch.com:

  • ~ becomes ~0
  • / becomes ~1
-- Aldo 'xoen' Giambelluca
Source: StackOverflow