0
0
Kubernetesdevops~5 mins

kubectl delete for removal in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you need to remove resources like pods or deployments from your Kubernetes cluster to free up space or stop unwanted services. The kubectl delete command helps you safely remove these resources.
When you want to stop a running application by deleting its pod.
When you need to remove a deployment that is no longer needed.
When cleaning up test resources after experimenting with Kubernetes objects.
When you want to delete a service that is no longer exposing your app.
When you want to delete a namespace and all resources inside it.
Commands
This command deletes the pod named 'my-pod' from the cluster, stopping the running container inside it.
Terminal
kubectl delete pod my-pod
Expected OutputExpected
pod "my-pod" deleted
This command lists all pods to verify that 'my-pod' has been removed.
Terminal
kubectl get pods
Expected OutputExpected
No resources found in default namespace.
This command deletes the deployment named 'example-deployment', which also removes all pods managed by it.
Terminal
kubectl delete deployment example-deployment
Expected OutputExpected
deployment.apps "example-deployment" deleted
This command lists all deployments to confirm that 'example-deployment' has been deleted.
Terminal
kubectl get deployments
Expected OutputExpected
No resources found in default namespace.
Key Concept

If you remember nothing else from this pattern, remember: kubectl delete removes the specified Kubernetes resource immediately from your cluster.

Common Mistakes
Trying to delete a resource that does not exist.
kubectl will return an error because it cannot find the resource to delete.
Use kubectl get to check the exact resource name before deleting.
Deleting a deployment but expecting pods to remain running.
Deleting a deployment also deletes all pods it manages, so the app stops running.
If you want to stop pods without deleting the deployment, scale the deployment to zero replicas instead.
Not specifying the resource type when deleting.
kubectl delete needs the resource type (pod, deployment, service) to know what to remove.
Always specify the resource type before the resource name, like 'kubectl delete pod my-pod'.
Summary
Use 'kubectl delete' with the resource type and name to remove Kubernetes objects.
Verify deletion by listing resources with 'kubectl get'.
Deleting a deployment removes all pods it manages immediately.