1. Overview

We can’t think of a distributed system without networking, and Kubernetes is no exception. In Kubernetes, a Service is a primary networking object. It provides a fixed DNS record along with a fixed IP address and port number.

As Kubernetes users, we often need to delete unwanted Service objects. This helps us to tidy up the cluster. In this quick tutorial, we’ll discuss various methods to delete a Kubernetes service. So, let’s get started.

2. Setting up an Example

To begin with, let’s create a few Kubernetes Service objects of different types.

It’s good practice to keep all related objects in a logical group. In Kubernetes, we achieve this using Namespaces.

Namespaces allow us to isolate resources within a logical boundary. So, let’s create a new namespace with the name service-demo:

$ kubectl create ns service-demo
namespace/service-demo created

Now, let’s create a declarative configuration and save it in a service-demo.yaml file:

apiVersion: v1
kind: Service
metadata:
  name: cluster-ip-01
  namespace: service-demo
  labels:
    app: nginx
spec:
  type: ClusterIP 
  ports:
  - name: http
    port: 80
    targetPort: 8080
---
apiVersion: v1
kind: Service
metadata:
  name: cluster-ip-02
  namespace: service-demo
  labels:
    app: nginx
spec:
  type: ClusterIP 
  ports:
  - name: http
    port: 80
    targetPort: 8080
---
apiVersion: v1 
kind: Service 
metadata:
  name: node-port-01
  namespace: service-demo
  labels:
    app: java
spec:
  type: NodePort
  ports:
    - name: http
      port: 80
      targetPort: 8080
---
apiVersion: v1
kind: Service
metadata:
  name: node-port-02
  namespace: service-demo
  labels:
    app: java
spec:
  type: NodePort
  ports:
    - name: http
      port: 80
      targetPort: 8080
---
apiVersion: v1
kind: Service
metadata:
  name: load-balancer-01
  namespace: service-demo
  labels:
    app: mysql
spec:
  type: LoadBalancer
  ports:
    - name: http
      port: 80
      targetPort: 8080
---
apiVersion: v1
kind: Service
metadata:
  name: load-balancer-02
  namespace: service-demo
  labels:
    app: mysql
spec:
  type: LoadBalancer
  ports:
    - name: http
      port: 80
      targetPort: 8080

Next, let’s create the services using the apply command:

$ kubectl apply -f service-demo.yaml

Now, the required setup is ready. In the next few sections, we’ll see how to delete these Service objects.

3. Deletion Using the Service Name

One of the easiest methods to delete a Kubernetes object is using its name. So, let’s see a few practical examples of deleting a single service and multiple services using their names.

3.1. Deleting a Single Service

We can use the delete operation with the kubectl command to delete any Kubernetes object.

To understand this, let’s delete the service with the name cluster-ip-01:

$ kubectl delete service cluster-ip-01 -n service-demo
service "cluster-ip-01" deleted

Here, the output message confirms that the service cluster-ip-01 has been deleted.

3.2. Deleting Multiple Services at Once

In the previous example, we saw how easy it is to delete a service using its name. However, this method isn’t the most efficient if we want to delete multiple services. In such cases, we can specify multiple service names on the command line.

So, let’s delete the services with the names node-port-01 and node-port-02:

$ kubectl delete service node-port-01 node-port-02 -n service-demo
service "node-port-01" deleted
service "node-port-02" deleted

3.3. Deleting All Services at Once

So far, we’ve discussed how to delete a single service and multiple services using their names. However, those methods aren’t suitable if we want to delete all services. It quickly becomes time-consuming if the services to be deleted are in large numbers. In such cases, we can use the –all option to delete all services.

Let’s delete all services from the service-demo namespace:

$ kubectl delete service --all -n service-demo

Here, we can see that the single command is sufficient to delete all the services from a particular namespace.

It’s important to note that this operation is destructive and can bring downtime if it’s performed carelessly. So, we must be very careful while executing this command.

4. Deletion Using the Declarative Configuration

While setting up our example, we used declarative configuration to create Kubernetes services. Similarly, we can also use the same declarative configuration to delete services. Let’s see this in action.

In the previous section, we deleted all services. So, first, let’s recreate them:

$ kubectl apply -f service-demo.yaml

4.1. Providing Declarative Configuration From Stdin

In the service-demo.yaml file, the first 13 lines represent the configuration of the cluster-ip-01 service. We can specify this declarative configuration to the -f option via the standard input stream:

$ head -13 service-demo.yaml | kubectl delete -f -
service "cluster-ip-01" deleted

In this example, the head command displays the first 13 lines of the service-demo.yaml file. Then, the shell pipes the output to the kubectl command. The hyphen (-) character in the command represents the standard input stream.

4.2. Providing Declarative Configuration From File

Similarly, we can also specify the declarative configuration using a plain text file.

To understand this, let’s delete all the services defined in the service-demo.yaml file:

$ kubectl delete -f service-demo.yaml --ignore-not-found=true -n service-demo

Here, we’ve used the –ignore-not-found=true option to suppress the error. The error is generated when the command tries to delete the non-existing cluster-ip-01 service.

5. Deletion Using the Field Selector

In Kubernetes, field selectors allow us to filter the objects based on certain fields. For example, we can use the metadata.name or metadata.namespace fields to filter the Service objects.

Let’s understand this with a simple example, but before that, let’s recreate all services:

$ kubectl apply -f service-demo.yaml

With field selectors, we can use the equality as well as inequality operators:

$ kubectl delete service --field-selector metadata.name==cluster-ip-02 -n service-demo
service "cluster-ip-02" deleted

In this example, we’ve used the metadata.name field to match the service name. The == operator in the command represents the equality condition.

6. Deletion Using the Label Query

In the previous section, we saw the usage of the field selectors. However, the field selector’s functionality isn’t as rich as labels because the supported fields vary by Kubernetes resource type. In addition to this, they don’t support the set-based conditions. To overcome this limitation, we can use Kubernetes labels.

Primarily, labels are used to tag resources, and they’re defined in key-value pairs. For example, while setting up an example, we added labels, such as – app: nginx, app: java, and app: mysql.

Let’s use the –show-labels option to display the labels:

$ kubectl get service --show-labels -n service-demo
NAME               TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE   LABELS
cluster-ip-01      ClusterIP      10.96.137.225   <none>        80/TCP         28s   app=nginx
load-balancer-01   LoadBalancer   10.96.19.90     <pending>     80:30655/TCP   28s   app=mysql
load-balancer-02   LoadBalancer   10.96.228.184   <pending>     80:30061/TCP   28s   app=mysql
node-port-01       NodePort       10.96.121.44    <none>        80:31406/TCP   28s   app=java
node-port-02       NodePort       10.96.248.95    <none>        80:32472/TCP   28s   app=java

Here, the last column shows the labels associated with each service. Now, let’s see how to use these labels to delete the service.

6.1. Using a Single Label

We can use the –selector option to specify the label query.

To understand this, let’s delete all the services in which the label key is app, and its value is java:

$ kubectl delete service --selector app=java -n service-demo
service "node-port-01" deleted
service "node-port-02" deleted

In this example, the assignment operator (=) represents the equality-based condition. Similarly, we can use the not-equal-to operator (!=) to specify the inequality-based condition.

6.2. Using Multiple Labels

In the previous section, we used the simple equality-based condition. Similarly, we can also use the set-based conditions with labels to perform advanced filtering.

So, let’s use the in operator to delete all services whose label key is app and label value is either nginx or mysql:

$ kubectl delete service --selector 'app in(nginx,mysql)' -n service-demo
service "cluster-ip-01" deleted
service "load-balancer-01" deleted
service "load-balancer-02" deleted

7. Cleaning Up

It’s a good practice to remove the temporarily created objects. This helps us to utilize the cluster resources efficiently.

We already removed all Service objects. Now, let’s remove the Namespace object that we created while setting up our example:

$ kubectl delete ns service-demo
namespace "service-demo" deleted

8. Conclusion

In this article, we discussed how to delete services from a Kubernetes cluster.

First, we discussed how to delete a service using a declarative configuration. Next, we saw how to delete a single service and multiple services using their names.

Finally, we discussed how to use the field selectors and labels.