How to delete a node label by command and api?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Node labels in Kubernetes are key-value pairs used to identify attributes and assist in scheduling decisions. They help to define the characteristics of nodes and can be used to bind pods to specific nodes, facilitate complex deployments, or partition workloads effectively. There are occasions, however, where removing a label is necessary. This article provides guidance on how to delete a node label using both command-line interface (CLI) and API methods. We'll delve into technical explanations and include examples to ensure clarity.
Removing Node Labels Using Command-Line
Prerequisite
Before performing these actions, ensure you have access to a running Kubernetes cluster and have kubectl installed and configured to interact with it.
Step-by-Step Guide
- Identify the Node: First, list all nodes and locate the one from which you want to delete the label.
Example output:
- View Current Labels: To see the current labels of a specific node, use:
Example:
- Remove the Label: Use the following command to delete a label from a node:
Example:
This command removes the label env from node-1.
Removing Node Labels Using API
Prerequisite
Access to the Kubernetes API server is necessary. Familiarity with tools like curl or HTTP client libraries will be beneficial.
API Request Structure
- Define the HTTP Method and Endpoint: To delete a label using the API, you will use the HTTP method
PATCHon the node resource.Endpoint format:
- Create the Patch Data: The patch request must specify the
Content-Typeasapplication/json-patch+jsonand define the operations for removal.Example JSON Patch:
- Execute the HTTP Request: Use your client of choice to send the PATCH request. Here's how you might use
curl:
Summary Table
| Method | Tool Used | HTTP Method | Required Permission | Example Command/Request |
| Command-Line | kubectl | N/A | Describe & Modify | kubectl label nodes node-1 env- |
| API | curl | PATCH | API Access Token | curl -XPATCH -H "Content-Type: application/json-patch+json" -d '[{"op": "remove", "path": "/metadata/labels/env"}]' https://<api-server>/api/v1/nodes/node-1 |
Additional Considerations
- Security: Ensure that only authorized personnel or service accounts have permissions to alter node labels. Use role-based access control (RBAC) judiciously.
- Impact Assessment: Understand how removing a label might affect workloads scheduled on a node. Labels often dictate scheduling preferences or constraints.
- Version Compatibility: APIs and specifications can vary between Kubernetes versions. Validate the compatibility of commands and API endpoints with your cluster version.
By following these guidelines and examples, you can properly manage node labels, helping maintain an organized and efficient Kubernetes environment.
Related reading
- How to delete all resources from Helm list by one command?
- How to delete all resources from Kubernetes one time?
- How to delete completed kubernetes pod?
- How to Delete Kubernetes Service
- How to delete kafka consumer group (created via new consumer api)?
- How to delete Kafka topic using Kafka REST Proxy?
- How to delete node in EKS managed node group if the Kubelet crashes or stops reporting?
- How to delete persistent volumes in Kubernetes

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.