In this article, we will explore how to delete the resources process in different steps with the help of proper examples. You must know the syntax of this command that is entered on the kubectl command line tool. By the end of this article, you will be able to understand the working and usage of this command in your Kubernetes applications.
What Is the Kubectl Delete Command?
The kubectl command-line tool provides a command through which we can delete the specific and wanted resource from our Kubernetes cluster and that is the “kubectl delete” command. The resources of Kubernetes include pods, services, deployments, etc. In Kubernetes, there is no option to resume or hold the resources in Kubernetes. So, we can delete the unwanted resources from our Kubernetes application easily by running this command.
First, open your Linux operating system terminal on which we run our desired commands. Once we run the “delete” command, we cannot redo the changes or pause the deletion process related to our resources. Let’s start the process which we will explain to you step by step.
Step 1: Launch the Minikube Cluster Dashboard
This is the first step where we learn how we can create a new cluster in our Kubernetes container. We just run the following command to create or start the minikube which is the dashboard of our Kubernetes cluster. The command is mentioned here:
Run the command. When the command execution is completed, a new minikube cluster is installed in our Kubernetes application that contains the name of container, pods, stateSets, and all the configuration setting that is required to run the Kubernetes applications efficiently. Make sure that the kubectl command line tool is installed in your Kubernetes application system.
Step 2: Fetch the Detailed Information of the Kubernetes Cluster
In this step, we run the “get all” command on the kubectl tool. We will see how we can easily get the details of all the pods and services, State Set, and pod auto scaling that are running in our Kubernetes container. For this purpose, we run the following command that is mostly used in daily developers’ development:
After the execution of the command, the result of this command appears in our terminal that we attached in the following. We can see that all the pod’s information and services information are fetched and displayed.
This is the output of all pods that are in running the state, their ready states, age, and many more.
The result of all pods appears successfully through this command. We learned the detailed information about services, StateSet, and autoscaling. In this information, the name; desired, current, active, and ready state; age of service; and all other relevant options are in the Kubernetes container.
Step 3: Delete the Deployment from the Kubernetes Cluster
In this step, we will show you how you can delete any resource from the Kubernetes cluster. Here, we run the kubectl command that is mentioned in the following to delete the resources accurately.
When this command is run and completes its execution, the result is shown in our terminal. In this “delete deployment” command, we want to delete the deployment from the Kubernetes cluster whose name is “php”. So, as seen in the following screenshot, we can see that “php” is deleted from the deployment folder in our Kubernetes cluster:
Step 4: Delete the Service from the Kubernetes Cluster
In this step, we will learn how we can delete the service that is not in use from our Kubernetes cluster. We must know the name of the service that needs to be deleted. Please make sure that the service that we delete is the right one because once a resource is deleted, it never recovers or is redone. We run the following command. This deletes the service on the kubectl tool of Kubernetes:
When the command is executed, the output is displayed on your terminal. For your understanding, the screenshot is attached in the following. Here, we delete the service whose name is “php”. The output displays that the“php” service is deleted:
Step 5: Retrieve the Services from the Kubernetes Cluster
In this step, we will learn how we can retrieve the list of services from the Kubernetes cluster to confirm the deletion of services. We run the following command on the kubectl tool to get the running services in the Kubernetes cluster.
When the command is executed, all the information related to running services is displayed on the terminal. The result of this “get services” command is attached in the previous screenshot. In this example, we fetch one service whose name is “Kubernetes”, the type is “Cluster IP”, the IP address is “10.96.0.1”, the external IP value is “none”, the connect PORT is “443/TCP”, and the age of this service is “18days”.
Step 6: Delete the Pod from the Kubernetes Cluster
In this last step, we will learn how we can delete unwanted pods from our Kubernetes cluster without distrusting the whole Kubernetes system.
After the command execution is completed, the pod that we want is removed from our cluster. Like in the given command, we delete the pod name which is “nginx1”. The output of this command is attached in the following as a screenshot for your better understanding. The result that is displayed on the terminal against this command is “pod “nginx1” deleted”.
We learned how we can delete the deployments, services, and pods from our Kubernetes system permanently. You can also delete multiple resources from your cluster by mentioning the name of the resources and all are separated through space. This command helps us to tidy our cluster resources to make the system more secure and error-free. You can also delete your unwanted resources from your Kubernetes application by following this article and practicing these examples in your programs.