Delete all pods when starts for a word (uncomplete name of the pod)

7/24/2020

I have some projects in OC, and the names of the pods are generated in base of a chain and a commit. So, I want create an instruction in AzureDevOps to delete all the pods by an incomplete name the name with an specific characters, but finishing with others.

Example:

root@oc-jump-pod:/# oc get po
NAME                           READY     STATUS    RESTARTS   AGE
podA-74rt7                     1/1       Running   0          20h
podB-6744849c59                1/1       Running   0          20h
podB-6746378213                1/1       Running   0          20h

I need use something like:

oc delete po podB*
Error from server (NotFound): pods "podB*" not found

How can I filter the deletion with a couple of characters and not the complete name of the pod?

DeployConfig added:

root@oc-jump-pod-pre:/# oc describe deploy NAME
Name:                   NAME
Namespace:              NAME-pre
CreationTimestamp:      Mon, 25 May 2020 07:01:14 +0000
Labels:                 app.kubernetes.io/instance=NAME
                        app.kubernetes.io/managed-by=Helm
                        app.kubernetes.io/name=NAME
                        app.kubernetes.io/version=latest
                        helm.sh/chart=NAME-1.0.0
Annotations:            deployment.kubernetes.io/revision=3
                        meta.helm.sh/release-name=NAME
                        meta.helm.sh/release-namespace=sda-NAME-pre
Selector:               app.kubernetes.io/instance=NAME,app.kubernetes.io/name=NAME
Replicas:               1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:           app.kubernetes.io/instance=NAME
                    app.kubernetes.io/name=NAME
  Service Account:  default
  Containers:
   NAME:
    Image:  registry/sda-NAME-dev/test-NAME-java:0.0.2
    Port:   8080/TCP
    Limits:
      cpu:     150m
      memory:  1444Mi
    Requests:
      cpu:     100m
      memory:  1Gi
    Environment:
      APP_NAME:                     NAME
      JAVA_OPTS_EXT:                ....
      SPRING_CLOUD_CONFIG_PROFILE:  pre
      TZ:                           Europe/Madrid
      WILY_MOM_PORT:                5001
      spring_application_name:      NAME_pre
      spring_cloud_config_uri:      https://config.d.cluster.local
    Mounts:
      /etc/jks from jks (ro)
      /etc/secret-vol from secretvol (ro)
      /etc/truststore from jssecacerts (ro)
  Volumes:
....
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Progressing    True    NewReplicaSetAvailable
  Available      True    MinimumReplicasAvailable
OldReplicaSets:  NAME-6744849c59 (1/1 replicas created)
NewReplicaSet:   <none>
Events:          <none>
-- RCat
kubernetes
openshift

3 Answers

7/24/2020

For regex based approach following commands will delete all the pods starting with "podB"

oc get pods | awk '/^podB/{system("oc delete pod " $1)}'

Anyway i would recommend using the method provided by Dashrath Mundkar. For openshift you do not need to provide any namespace if you just want to access resource in your current project so you can just remove the "-n namespace" from the commands like

oc get pod -l labelname=value
oc delete pod -l labelname=value

Just make sure the labels you provided are unique to pods you want to delete

-- Vishal
Source: StackOverflow

1/20/2021

I use the below command to delete pods in bulk. This single command helps me to delete selective pods of my choice. It is really handy and fast and saves lot of time. Happy coding!

oc get pods -o wide -n <namespace> | grep -i -E "<search_string_1>|<search_string_2>" | grep -v "<exclude_results_with_search_string_3>" | awk '{print $1}' | xargs oc delete pod
-- Vikd
Source: StackOverflow

7/24/2020

First get pod name if it belongs to deploymentConfig like this, I don't think using wildcard you can delete pods, I would suggest use labels and selectors for that.

oc get po -n namespace -l labelname=value

then delete those pods

oc delete po -l labelname=value -n namespace
-- Dashrath Mundkar
Source: StackOverflow