Monday 13 August 2018

Groovy Code To Force Delete Multiple Pods From Kubernetes

ad300
Advertisement
There might be a situation where you have 10 to 20 Pods listed in "terminating status" even when you have issued a delete namespace command. So Now, You may need to force delete all of them. 

But deleting them manually could be a cumbersome job and needs a lot of patience.

Well, there is a way where you can deal with this situation as well:







Code 1➤ Groovy Code to force delete multiple pods
Create a folder and save below code to your sample.groovy file. Now open the command prompt or git bash and navigate to the folder where you have kept your sample.groovy code.

Now run the below command.

➤ groovy sample.groovy

NOTE: you have to install groovy if it is not installed already


nameSpace="yournamespace"
getPods="kubectl get pods -o=name -n "+nameSpace+""pods = getPods.execute().text

if(!pods.allWhitespace && !pods.equals("No resources found.")){

    def podNames = pods.split('\n')
    for (int i=0; i <  podNames.length; i++){
        command  = "kubectl delete pod "+""+podNames[i].replace( 'pod/', '' ).trim()+""+" -n "+nameSpace+" --grace-period=0 --force"        println command.execute().text
    }

}else{
    println "No Pods Listed"}
Code 2➤ Shell Script Code to force delete multiple pods



Share This
Previous Post
Next Post

Welcome to my blog, I am Subhash Junas, I have been working on Core Java, Ruby, Selenium, Cucumber and on various automation technologies from past couple of years. I love to work on new challenges, solving problems and obstacles.

0 comments: