Kubernetes
POD Management
Pattern Matching
Wildcard Usage
Command Line Operations

kubectl delete pods by name pattern, wildcard or label

Codemia Knowledge Hub ·

kubectl delete pod does not accept a wildcard directly. Either list pod names, filter them with grep, and pass the result to kubectl delete pod, or, better, delete by label with kubectl delete pods -l <label>.

A Pod is the smallest deployable unit in Kubernetes. It can hold one or more tightly coupled containers, storage resources, a unique network IP, and options for how the containers run. Common reasons to delete pods are freeing cluster resources, removing older pods during an update, and clearing faulty pods while troubleshooting.

Delete pods whose names match a pattern

To delete all pods whose names start with "frontend-", filter the names with grep:

bash
kubectl delete pod $(kubectl get pods -o jsonpath='{.items[*].metadata.name}' | tr ' ' '\n' | grep '^frontend-')

How it works:

  1. Retrieve pod names: kubectl get pods -o jsonpath='&#123;.items[*].metadata.name&#125;' lists all pod names.
  2. Format output: tr ' ' '\n' turns the space-separated output into one name per line.
  3. Pattern matching: grep '^frontend-' keeps names that start with "frontend-".
  4. Delete pods: the filtered names are passed to kubectl delete pod.

Delete pods by label

Labels are key/value pairs attached to objects such as pods, used to identify and organize resources. If all your frontend pods carry tier=frontend:

bash
kubectl delete pods -l tier=frontend

The -l flag specifies the label selector. This is quick and does not require parsing pod names.

Limit deletion to a namespace

Namespaces partition resources into separate sections of a cluster. Combine a namespace with a label selector:

bash
kubectl delete pods -n mynamespace -l app=myapp

-n mynamespace restricts the deletion to "mynamespace", and -l app=myapp selects the pods inside it.

Before you delete

  • Pods owned by a Deployment come back: Kubernetes recreates pods that a Deployment manages. To scale down, adjust the replica count instead.
  • Graceful termination: Kubernetes terminates pods gracefully by default. Force deletion with --force and --grace-period=0 if necessary.
  • Dry run: append --dry-run=client to preview which pods would be deleted without removing them.
FeatureDescription
Bash WildcardUse grep with regex for pattern matching pod names easily.
Label-based DeletionUse labels attached to pods for more precise and readable deletion.
Namespace ScopeRestrict deletion to specific namespaces with -n.
Graceful TerminationBy default, pods are gracefully terminated unless forced.
Dry RunTest the deletion command with --dry-run=client.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design