Delete Pods Forcefully
Utilizing the kubectl get rid of command when combined with the –grace-period=0 parameter allows you to swiftly destroy pods in Kubernetes. With a zero-second grace time provided by this flag, here will be no waiting period until pods end gracefully. Here’s how to achieve it:
kubectl delete pods --all --grace-period=0 --force
With this command, each pod in the current namespace will be forcefully removed. The -n contention can be utilized to denote the namespace if you want to get rid of pods from that namespace:
kubectl delete pods --all --grace-period=0 --force -n your-namespace
Enter the name of the namespace from which you wish to delete all pods in place of your-namespace.
Kubernetes – Kubectl Delete
Kubernetes is an open-source Container Management tool that automates container deployment, container scaling, descaling, and container load balancing (also called a container orchestration tool). It is written in Golang and has a huge community because it was first developed by Google and later donated to CNCF (Cloud Native Computing Foundation). kubectl delete is used to delete resources by using a configuration file or by using the type of resource and the resource name.
kubectl delete ([-f FILENAME] | TYPE [(NAME | --all)])
- Delete by Configuration File (
-f FILENAME
):-f FILENAME
: Specifies a configuration file containing the resource definition(s) to be deleted.- This mode allows you to delete resources defined in a YAML or JSON file. You provide the path to the file after the
-f
flag, andkubectl
deletes the resources defined in that file.
- Delete by Resource Type and Name (
TYPE [(NAME | --all)]
):TYPE
: Specifies the type of resource to be deleted (e.g., deployment, service, pod, etc.).NAME
: Optionally, specifies the name of the specific resource instance to be deleted.--all
: Deletes all instances of the specified resource type across namespaces.
Example: Suppose we are having Nginx web server deployment and Service running.
This is the nginx service manifest file.
Here is the all running resources on the default namespace. Refer the below image for your reference.
Deleting Deployment
To delete the resources in Kubernetes, the kubectl delete command provides the flexibility. We can delete resources by specifying a configuration file or directly by resource type and name. For instance, to delete a Deployment named deployment_name, you execute the below command:
$ kubectl delete deployment deployment_name
Alternatively, you can also point your terminal to the file containing the deployment config file and use the command
$ kubectl delete -f your_config_file.yaml
Deleting Service
The same principle applies to deleting Services. To delete a Service named service_name
, you use:
$ kubectl delete service service_name
Alternatively, you can also point your terminal to the file containing the deployment config file and use the command
$ kubectl delete -f your_config_file.yaml