Learning Kubernetes - Episode 9 - Deleting Pods in Kubernetes

Learning Kubernetes - Episode 9 - Deleting Pods in Kubernetes

In this episode, we'll discuss various methods for deleting Pods in Kubernetes. We'll learn how to delete Pods by name, label selector, namespace, and other advanced techniques.

Arman Dwi Pangestu
Arman Dwi PangestuMarch 12, 2026
0 views
10 min read

Introduction

Note

If you want to read the previous episode, you can click the Episode 8 thumbnail below

Episode 8Episode 8

In the previous episode, we learned deeply about the Namespace object in Kubernetes. In episode 9, we'll discuss an important operational task: Deleting Pods in Kubernetes.

Note: Here I'll be using a Kubernetes Cluster installed through K3s.

While we learned how to create and manage Pods in episode 5, knowing how to properly delete Pods is equally important. In production environments, you'll frequently need to delete Pods for various reasons: cleaning up resources, troubleshooting issues, performing maintenance, or managing deployments. Understanding different deletion methods and their implications is crucial for effective cluster management.

Why Do We Need to Delete Pods?

Before diving into the how, let's understand why you might need to delete Pods:

  • Resource cleanup - Remove Pods that are no longer needed to free up cluster resources
  • Troubleshooting - Delete and recreate Pods to resolve issues
  • Configuration updates - Remove old Pods after updating configurations
  • Testing - Clean up test Pods after experiments
  • Maintenance - Remove Pods before node maintenance or upgrades
  • Failed Pods - Clean up Pods that are in error states
  • Scaling down - Reduce the number of running Pods

Understanding Pod Deletion

When you delete a Pod in Kubernetes, several things happen:

  1. Termination signal - Kubernetes sends a TERM signal to the containers in the Pod
  2. Grace period - Containers have a grace period (default 30 seconds) to shut down gracefully
  3. Force kill - After the grace period, if containers are still running, they're forcefully killed
  4. Resource cleanup - Associated resources are cleaned up
  5. Pod removal - The Pod is removed from the cluster

Pod Deletion States

During deletion, a Pod goes through these states:

  • RunningTerminatingDeleted

You can see the Terminating state when you run kubectl get pods during deletion:

Kubernetesbash
NAME        READY   STATUS        RESTARTS   AGE
nginx-pod   1/1     Terminating   0          5m

Basic Pod Deletion Methods

Let's explore different methods to delete Pods:

Method 1: Delete Pod by Name

The most straightforward way to delete a Pod is by specifying its name:

Kubernetesbash
sudo kubectl delete pod <pod-name>

For example:

Kubernetesbash
sudo kubectl delete pod nginx-pod

Output:

Kubernetesbash
pod "nginx-pod" deleted

Method 2: Delete Pod in Specific Namespace

To delete a Pod in a specific Namespace, use the -n or --namespace flag:

Kubernetesbash
sudo kubectl delete pod <pod-name> -n <namespace>

For example:

Kubernetesbash
sudo kubectl delete pod nginx-pod -n development

Method 3: Delete Multiple Pods by Name

You can delete multiple Pods at once by listing their names:

Kubernetesbash
sudo kubectl delete pod <pod-name-1> <pod-name-2> <pod-name-3>

For example:

Kubernetesbash
sudo kubectl delete pod nginx-pod-1 nginx-pod-2 nginx-pod-3

Method 4: Delete Pod Using YAML File

If you created a Pod using a YAML file, you can delete it using the same file:

Kubernetesbash
sudo kubectl delete -f pod.yml

This is useful because:

  • You don't need to remember the Pod name
  • It deletes all resources defined in the file
  • It's consistent with how you created the resource

Deleting Pods by Label Selector

One of the most powerful features is deleting Pods based on their labels. This is especially useful when you have many Pods with similar characteristics.

Delete Pods with Specific Label

To delete all Pods with a specific label:

Kubernetesbash
sudo kubectl delete pod -l <key>=<value>

For example, delete all Pods with app=nginx:

Kubernetesbash
sudo kubectl delete pod -l app=nginx

Delete Pods with Multiple Labels

You can use multiple label selectors with comma separation (AND logic):

Kubernetesbash
sudo kubectl delete pod -l app=nginx,environment=development

This deletes all Pods that have both app=nginx AND environment=development labels.

Delete Pods Using Set-Based Selectors

You can use more advanced selectors:

Kubernetesbash
# Delete Pods where environment is either development or staging
sudo kubectl delete pod -l "environment in (development,staging)"
 
# Delete Pods where environment is NOT production
sudo kubectl delete pod -l "environment notin (production)"
 
# Delete Pods that have the tier label
sudo kubectl delete pod -l tier
 
# Delete Pods that don't have the tier label
sudo kubectl delete pod -l "!tier"

Practical Example: Label-Based Deletion

Let's create multiple Pods and delete them using labels:

Step 1: Create Pods with labels

Kuberneteslabeled-pods.yml
apiVersion: v1
kind: Pod
metadata:
    name: nginx-dev-1
    labels:
        app: nginx
        environment: development
        tier: frontend
spec:
    containers:
        - name: nginx
          image: nginx
---
apiVersion: v1
kind: Pod
metadata:
    name: nginx-dev-2
    labels:
        app: nginx
        environment: development
        tier: backend
spec:
    containers:
        - name: nginx
          image: nginx
---
apiVersion: v1
kind: Pod
metadata:
    name: nginx-prod-1
    labels:
        app: nginx
        environment: production
        tier: frontend
spec:
    containers:
        - name: nginx
          image: nginx

Apply the configuration:

Kubernetesbash
sudo kubectl apply -f labeled-pods.yml

Step 2: Verify Pods are running

Kubernetesbash
sudo kubectl get pods -L app,environment,tier

Output:

Kubernetesbash
NAME           READY   STATUS    RESTARTS   AGE   APP     ENVIRONMENT    TIER
nginx-dev-1    1/1     Running   0          30s   nginx   development    frontend
nginx-dev-2    1/1     Running   0          30s   nginx   development    backend
nginx-prod-1   1/1     Running   0          30s   nginx   production     frontend

Step 3: Delete only development Pods

Kubernetesbash
sudo kubectl delete pod -l environment=development

Output:

Kubernetesbash
pod "nginx-dev-1" deleted
pod "nginx-dev-2" deleted

Step 4: Verify only production Pod remains

Kubernetesbash
sudo kubectl get pods

Output:

Kubernetesbash
NAME           READY   STATUS    RESTARTS   AGE
nginx-prod-1   1/1     Running   0          2m

Deleting All Pods in a Namespace

To delete all Pods in a specific Namespace:

Kubernetesbash
sudo kubectl delete pods --all -n <namespace>

For example:

Kubernetesbash
sudo kubectl delete pods --all -n development

Warning

Be very careful with this command! It will delete ALL Pods in the specified Namespace. Always double-check the Namespace before running this command.

Delete All Pods in Default Namespace

To delete all Pods in the default Namespace:

Kubernetesbash
sudo kubectl delete pods --all

Force Deleting Pods

Sometimes Pods get stuck in the Terminating state and won't delete normally. In these cases, you can force delete them.

Force Delete with Grace Period 0

To force delete a Pod immediately:

Kubernetesbash
sudo kubectl delete pod <pod-name> --grace-period=0 --force

For example:

Kubernetesbash
sudo kubectl delete pod nginx-pod --grace-period=0 --force

Warning

Force deleting Pods should be used as a last resort. It doesn't give containers time to shut down gracefully, which can lead to data loss or corruption. Only use this when a Pod is stuck and won't delete normally.

Understanding Grace Period

The grace period is the time Kubernetes gives containers to shut down gracefully:

  • Default grace period: 30 seconds
  • Custom grace period: You can specify a different value
  • Grace period 0: Immediate termination (force delete)

Example with custom grace period:

Kubernetesbash
# Give 60 seconds for graceful shutdown
sudo kubectl delete pod nginx-pod --grace-period=60

Deleting Pods with Specific Status

You can combine kubectl commands with other tools to delete Pods based on their status.

Delete All Failed Pods

To delete all Pods with Failed status:

Kubernetesbash
sudo kubectl delete pod --field-selector=status.phase=Failed

Delete All Succeeded Pods

To delete all Pods with Succeeded status (completed jobs):

Kubernetesbash
sudo kubectl delete pod --field-selector=status.phase=Succeeded

Delete Pods in Specific Phase

You can use field selectors to target Pods in specific phases:

Kubernetesbash
# Delete Pods in Pending state
sudo kubectl delete pod --field-selector=status.phase=Pending
 
# Delete Pods in Unknown state
sudo kubectl delete pod --field-selector=status.phase=Unknown

Practical Example: Cleanup Failed Pods

Let's create a scenario with failed Pods and clean them up:

Step 1: Create a Pod that will fail

Kubernetesfailing-pod.yml
apiVersion: v1
kind: Pod
metadata:
    name: failing-pod
spec:
    containers:
        - name: failing-container
          image: busybox
          command: ["sh", "-c", "exit 1"]
    restartPolicy: Never

Apply the configuration:

Kubernetesbash
sudo kubectl apply -f failing-pod.yml

Step 2: Wait for Pod to fail

Kubernetesbash
sudo kubectl get pods

Output:

Kubernetesbash
NAME          READY   STATUS   RESTARTS   AGE
failing-pod   0/1     Error    0          10s

Step 3: Delete all failed Pods

Kubernetesbash
sudo kubectl delete pod --field-selector=status.phase=Failed

Combining Deletion Methods

You can combine different methods for more precise deletion:

Delete Pods by Label in Specific Namespace

Kubernetesbash
sudo kubectl delete pod -l app=nginx -n development

Delete Pods by Label with Grace Period

Kubernetesbash
sudo kubectl delete pod -l app=nginx --grace-period=60

Delete Multiple Resources at Once

You can delete multiple resource types at once:

Kubernetesbash
# Delete Pods and Services with specific label
sudo kubectl delete pod,service -l app=nginx

Deleting Pods Managed by Controllers

When Pods are managed by controllers (Deployments, ReplicaSets, StatefulSets, DaemonSets), deleting the Pod directly will cause the controller to recreate it.

Understanding Controller Behavior

If you delete a Pod managed by a Deployment:

Kubernetesbash
sudo kubectl delete pod nginx-deployment-abc123

The Deployment will immediately create a new Pod to maintain the desired replica count.

Proper Way to Delete Controller-Managed Pods

To permanently delete Pods managed by controllers, you need to delete the controller itself:

Delete Deployment (and its Pods)

Kubernetesbash
sudo kubectl delete deployment nginx-deployment

Delete ReplicaSet (and its Pods)

Kubernetesbash
sudo kubectl delete replicaset nginx-replicaset

Delete StatefulSet (and its Pods)

Kubernetesbash
sudo kubectl delete statefulset nginx-statefulset

Delete DaemonSet (and its Pods)

Kubernetesbash
sudo kubectl delete daemonset nginx-daemonset

Scaling to Zero Instead of Deleting

Sometimes you want to temporarily stop Pods without deleting the controller:

Kubernetesbash
# Scale Deployment to 0 replicas
sudo kubectl scale deployment nginx-deployment --replicas=0

This stops all Pods but keeps the Deployment configuration. You can scale back up later:

Kubernetesbash
# Scale back to 3 replicas
sudo kubectl scale deployment nginx-deployment --replicas=3

Practical Scenarios

Let's explore some real-world scenarios for deleting Pods:

Scenario 1: Clean Up Test Environment

You've been testing in the testing Namespace and want to clean up:

Kubernetesbash
# Delete all Pods in testing Namespace
sudo kubectl delete pods --all -n testing
 
# Or delete the entire Namespace (including all resources)
sudo kubectl delete namespace testing

Scenario 2: Remove Old Version Pods

You've deployed a new version and want to remove old version Pods:

Kubernetesbash
# Delete Pods with old version label
sudo kubectl delete pod -l version=v1.0
 
# Keep Pods with new version
# (Pods with version=v2.0 remain untouched)

Scenario 3: Clean Up Failed Jobs

After running batch jobs, clean up completed and failed Pods:

Kubernetesbash
# Delete all succeeded Pods
sudo kubectl delete pod --field-selector=status.phase=Succeeded
 
# Delete all failed Pods
sudo kubectl delete pod --field-selector=status.phase=Failed

Scenario 4: Emergency Cleanup

A Pod is consuming too many resources and needs immediate removal:

Kubernetesbash
# Force delete immediately
sudo kubectl delete pod resource-hog-pod --grace-period=0 --force

Scenario 5: Selective Cleanup by Team

Different teams use the same cluster with label-based organization:

Kubernetesbash
# Backend team cleans up their development Pods
sudo kubectl delete pod -l team=backend,environment=development
 
# Frontend team cleans up their staging Pods
sudo kubectl delete pod -l team=frontend,environment=staging

Verification After Deletion

After deleting Pods, it's important to verify the deletion was successful:

Check Pod Status

Kubernetesbash
sudo kubectl get pods

Check Pods in All Namespaces

Kubernetesbash
sudo kubectl get pods -A

Check Specific Namespace

Kubernetesbash
sudo kubectl get pods -n development

Watch Pod Deletion in Real-Time

You can watch Pods being deleted in real-time:

Kubernetesbash
sudo kubectl get pods -w

This will continuously update the output as Pods change state. Press Ctrl+C to stop watching.

Common Mistakes and Pitfalls

Mistake 1: Deleting Pods Without Checking Namespace

Deleting Pods in the wrong Namespace can cause production issues.

Problem: You meant to delete test Pods but deleted production Pods instead.

Solution: Always verify the Namespace before deletion:

Kubernetesbash
# Check current Namespace
sudo kubectl config view --minify | grep namespace:
 
# Always specify Namespace explicitly
sudo kubectl delete pod nginx-pod -n development

Mistake 2: Force Deleting Without Understanding Impact

Force deleting Pods can cause data loss or corruption.

Problem: Force deleting a database Pod causes data corruption.

Solution: Only use force delete as a last resort. Try normal deletion first and investigate why the Pod won't terminate.

Mistake 3: Deleting Controller-Managed Pods Directly

Deleting Pods managed by controllers doesn't achieve the desired result.

Problem: You delete a Pod, but it immediately comes back.

Solution: Delete the controller (Deployment, ReplicaSet, etc.) or scale it to zero.

Mistake 4: Not Using Label Selectors Efficiently

Manually deleting Pods one by one is inefficient.

Problem: You have 50 test Pods to delete and you're deleting them individually.

Solution: Use label selectors to delete multiple Pods at once:

Kubernetesbash
sudo kubectl delete pod -l environment=test

Mistake 5: Deleting All Pods Without Backup

Deleting all Pods without having a way to recreate them.

Problem: You delete all Pods and realize you don't have the YAML configurations.

Solution: Always keep YAML configurations in version control (Git) before deleting resources.

Best Practices for Deleting Pods

Use Labels for Organized Deletion

Always label your Pods appropriately so you can delete them selectively:

Kubernetesyml
metadata:
    labels:
        app: myapp
        environment: development
        version: v1.0
        team: backend

Verify Before Deleting

Always verify what you're about to delete:

Kubernetesbash
# List Pods that will be deleted
sudo kubectl get pod -l app=nginx
 
# Then delete them
sudo kubectl delete pod -l app=nginx

Use Dry Run

Use the --dry-run flag to see what would be deleted without actually deleting:

Kubernetesbash
sudo kubectl delete pod -l app=nginx --dry-run=client

Document Deletion Reasons

When deleting Pods in production, document the reason:

Kubernetesbash
# Add a comment in your terminal history or runbook
# Reason: Cleaning up old version after successful v2.0 deployment
sudo kubectl delete pod -l version=v1.0

Use Namespace Isolation

Keep different environments in separate Namespaces to prevent accidental deletion:

Kubernetesbash
# Development Pods
sudo kubectl delete pod -l app=nginx -n development
 
# Production Pods are safe in different Namespace

Automate Cleanup with CronJobs

For regular cleanup tasks, use Kubernetes CronJobs:

Kubernetescleanup-cronjob.yml
apiVersion: batch/v1
kind: CronJob
metadata:
    name: cleanup-failed-pods
spec:
    schedule: "0 2 * * *"  # Run at 2 AM daily
    jobTemplate:
        spec:
            template:
                spec:
                    serviceAccountName: pod-cleaner
                    containers:
                        - name: kubectl
                          image: bitnami/kubectl:latest
                          command:
                              - /bin/sh
                              - -c
                              - kubectl delete pod --field-selector=status.phase=Failed
                    restartPolicy: OnFailure

Keep YAML Configurations

Always keep your Pod configurations in version control:

Kubernetesbash
# You can always recreate Pods from YAML
sudo kubectl apply -f pod.yml
 
# Safe to delete because you have the configuration
sudo kubectl delete -f pod.yml

Advanced Deletion Techniques

Delete Pods Older Than Specific Time

Using kubectl with jq to delete old Pods:

Kubernetesbash
# Delete Pods older than 7 days (requires jq)
sudo kubectl get pods -o json | jq -r '.items[] | select(.metadata.creationTimestamp | fromdateiso8601 < (now - 604800)) | .metadata.name' | xargs sudo kubectl delete pod

Delete Pods with Specific Resource Usage

You can script deletion based on resource usage:

Kubernetesbash
# Get Pods with high memory usage and delete them
# (This is an example - adjust thresholds as needed)
sudo kubectl top pods | awk '$3 > 1000 {print $1}' | xargs sudo kubectl delete pod

Batch Deletion with Confirmation

Create a script for safer batch deletion:

Kubernetessafe-delete.sh
#!/bin/bash
PODS=$(sudo kubectl get pod -l app=nginx -o name)
echo "The following Pods will be deleted:"
echo "$PODS"
read -p "Are you sure? (yes/no): " CONFIRM
if [ "$CONFIRM" = "yes" ]; then
    echo "$PODS" | xargs sudo kubectl delete
else
    echo "Deletion cancelled"
fi

Conclusion

In episode 9, we've explored various methods for deleting Pods in Kubernetes. We've learned how to delete Pods by name, label selector, Namespace, status, and using advanced techniques.

Key takeaways:

  • Use kubectl delete pod with various options for different deletion scenarios
  • Label selectors provide powerful filtering for bulk deletions
  • Field selectors allow deletion based on Pod status
  • Force deletion should be used as a last resort
  • Controller-managed Pods require deleting the controller, not the Pod directly
  • Always verify before deleting and use dry-run when possible
  • Keep YAML configurations in version control for easy recreation

Understanding how to properly delete Pods is essential for cluster maintenance, troubleshooting, and resource management. By mastering these techniques, you can efficiently manage your Kubernetes workloads and maintain a clean, well-organized cluster.

Are you getting a clearer understanding of Pod deletion in Kubernetes? In the next episode 10, we'll discuss another important Kubernetes concept: Probe. Probe is used to check the health of a Pod and restart it if it's not healthy.

Note

If you want to continue reading, you can click the Episode 10 thumbnail below

Episode 11Episode 11

Related Posts