How to delete completed kubernetes pod? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How do I parse command line arguments in Bash?How to check if a string contains a substring in BashHow to check if a program exists from a Bash script?How do I tell if a regular file does not exist in Bash?How do I split a string on a delimiter in Bash?How to count all the lines of code in a directory recursively?How do I reload .bashrc without logging out and back in?How to check if a variable is set in Bash?How to concatenate string variables in BashHow do I set a variable to the output of a command in Bash?

Random body shuffle every night—can we still function?

Google .dev domain strangely redirects to https

A term for a woman complaining about things/begging in a cute/childish way

What is the chair depicted in Cesare Maccari's 1889 painting "Cicerone denuncia Catilina"?

Is CEO the "profession" with the most psychopaths?

A proverb that is used to imply that you have unexpectedly faced a big problem

Why is a lens darker than other ones when applying the same settings?

What is a more techy Technical Writer job title that isn't cutesy or confusing?

Flight departed from the gate 5 min before scheduled departure time. Refund options

What is the difference between CTSS and ITS?

How can I prevent/balance waiting and turtling as a response to cooldown mechanics

Why complex landing gears are used instead of simple,reliability and light weight muscle wire or shape memory alloys?

Tips to organize LaTeX presentations for a semester

I can't produce songs

As a dual citizen, my US passport will expire one day after traveling to the US. Will this work?

Sally's older brother

Why not send Voyager 3 and 4 following up the paths taken by Voyager 1 and 2 to re-transmit signals of later as they fly away from Earth?

What does it mean that physics no longer uses mechanical models to describe phenomena?

Why is std::move not [[nodiscard]] in C++20?

Why are vacuum tubes still used in amateur radios?

GDP with Intermediate Production

Differences to CCompactSize and CVarInt

Where is the Next Backup Size entry on iOS 12?

How does the math work when buying airline miles?



How to delete completed kubernetes pod?



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How do I parse command line arguments in Bash?How to check if a string contains a substring in BashHow to check if a program exists from a Bash script?How do I tell if a regular file does not exist in Bash?How do I split a string on a delimiter in Bash?How to count all the lines of code in a directory recursively?How do I reload .bashrc without logging out and back in?How to check if a variable is set in Bash?How to concatenate string variables in BashHow do I set a variable to the output of a command in Bash?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















Answer my own question



I have a bunch of pods in kubernetes which are completed (successfully or unsuccessfully) and I'd like to clean up the output of kubectl get pods. Here's what I see when I run kubectl get pods:



NAME READY STATUS RESTARTS AGE
intent-insights-aws-org-73-ingest-391c9384 0/1 ImagePullBackOff 0 8d
intent-postgres-f6dfcddcc-5qwl7 1/1 Running 0 23h
redis-scheduler-dev-master-0 1/1 Running 0 10h
redis-scheduler-dev-metrics-85b45bbcc7-ch24g 1/1 Running 0 6d
redis-scheduler-dev-slave-74c7cbb557-dmvfg 1/1 Running 0 10h
redis-scheduler-dev-slave-74c7cbb557-jhqwx 1/1 Running 0 5d
scheduler-5f48b845b6-d5p4s 2/2 Running 0 36m
snapshot-169-5af87b54 0/1 Completed 0 20m
snapshot-169-8705f77c 0/1 Completed 0 1h
snapshot-169-be6f4774 0/1 Completed 0 1h
snapshot-169-ce9a8946 0/1 Completed 0 1h
snapshot-169-d3099b06 0/1 ImagePullBackOff 0 24m
snapshot-204-50714c88 0/1 Completed 0 21m
snapshot-204-7c86df5a 0/1 Completed 0 1h
snapshot-204-87f35e36 0/1 ImagePullBackOff 0 26m
snapshot-204-b3a4c292 0/1 Completed 0 1h
snapshot-204-c3d90db6 0/1 Completed 0 1h
snapshot-245-3c9a7226 0/1 ImagePullBackOff 0 28m
snapshot-245-45a907a0 0/1 Completed 0 21m
snapshot-245-71911b06 0/1 Completed 0 1h
snapshot-245-a8f5dd5e 0/1 Completed 0 1h
snapshot-245-b9132236 0/1 Completed 0 1h
snapshot-76-1e515338 0/1 Completed 0 22m
snapshot-76-4a7d9a30 0/1 Completed 0 1h
snapshot-76-9e168c9e 0/1 Completed 0 1h
snapshot-76-ae510372 0/1 Completed 0 1h
snapshot-76-f166eb18 0/1 ImagePullBackOff 0 30m
train-169-65f88cec 0/1 Error 0 20m
train-169-9c92f72a 0/1 Error 0 1h
train-169-c935fc84 0/1 Error 0 1h
train-169-d9593f80 0/1 Error 0 1h
train-204-70729e42 0/1 Error 0 20m
train-204-9203be3e 0/1 Error 0 1h
train-204-d3f2337c 0/1 Error 0 1h
train-204-e41a3e88 0/1 Error 0 1h
train-245-7b65d1f2 0/1 Error 0 19m
train-245-a7510d5a 0/1 Error 0 1h
train-245-debf763e 0/1 Error 0 1h
train-245-eec1908e 0/1 Error 0 1h
train-76-86381784 0/1 Completed 0 19m
train-76-b1fdc202 0/1 Error 0 1h
train-76-e972af06 0/1 Error 0 1h
train-76-f993c8d8 0/1 Completed 0 1h
webserver-7fc9c69f4d-mnrjj 2/2 Running 0 36m
worker-6997bf76bd-kvjx4 2/2 Running 0 25m
worker-6997bf76bd-prxbg 2/2 Running 0 36m


and I'd like to get rid of the pods like train-204-d3f2337c. How can I do that?










share|improve this question






















  • I had similar task but my duplicate pods are created by kubernetes job. in that case you only need deleted the job and the pods automatically deleted.

    – Lei Yang
    Mar 9 at 1:44

















0















Answer my own question



I have a bunch of pods in kubernetes which are completed (successfully or unsuccessfully) and I'd like to clean up the output of kubectl get pods. Here's what I see when I run kubectl get pods:



NAME READY STATUS RESTARTS AGE
intent-insights-aws-org-73-ingest-391c9384 0/1 ImagePullBackOff 0 8d
intent-postgres-f6dfcddcc-5qwl7 1/1 Running 0 23h
redis-scheduler-dev-master-0 1/1 Running 0 10h
redis-scheduler-dev-metrics-85b45bbcc7-ch24g 1/1 Running 0 6d
redis-scheduler-dev-slave-74c7cbb557-dmvfg 1/1 Running 0 10h
redis-scheduler-dev-slave-74c7cbb557-jhqwx 1/1 Running 0 5d
scheduler-5f48b845b6-d5p4s 2/2 Running 0 36m
snapshot-169-5af87b54 0/1 Completed 0 20m
snapshot-169-8705f77c 0/1 Completed 0 1h
snapshot-169-be6f4774 0/1 Completed 0 1h
snapshot-169-ce9a8946 0/1 Completed 0 1h
snapshot-169-d3099b06 0/1 ImagePullBackOff 0 24m
snapshot-204-50714c88 0/1 Completed 0 21m
snapshot-204-7c86df5a 0/1 Completed 0 1h
snapshot-204-87f35e36 0/1 ImagePullBackOff 0 26m
snapshot-204-b3a4c292 0/1 Completed 0 1h
snapshot-204-c3d90db6 0/1 Completed 0 1h
snapshot-245-3c9a7226 0/1 ImagePullBackOff 0 28m
snapshot-245-45a907a0 0/1 Completed 0 21m
snapshot-245-71911b06 0/1 Completed 0 1h
snapshot-245-a8f5dd5e 0/1 Completed 0 1h
snapshot-245-b9132236 0/1 Completed 0 1h
snapshot-76-1e515338 0/1 Completed 0 22m
snapshot-76-4a7d9a30 0/1 Completed 0 1h
snapshot-76-9e168c9e 0/1 Completed 0 1h
snapshot-76-ae510372 0/1 Completed 0 1h
snapshot-76-f166eb18 0/1 ImagePullBackOff 0 30m
train-169-65f88cec 0/1 Error 0 20m
train-169-9c92f72a 0/1 Error 0 1h
train-169-c935fc84 0/1 Error 0 1h
train-169-d9593f80 0/1 Error 0 1h
train-204-70729e42 0/1 Error 0 20m
train-204-9203be3e 0/1 Error 0 1h
train-204-d3f2337c 0/1 Error 0 1h
train-204-e41a3e88 0/1 Error 0 1h
train-245-7b65d1f2 0/1 Error 0 19m
train-245-a7510d5a 0/1 Error 0 1h
train-245-debf763e 0/1 Error 0 1h
train-245-eec1908e 0/1 Error 0 1h
train-76-86381784 0/1 Completed 0 19m
train-76-b1fdc202 0/1 Error 0 1h
train-76-e972af06 0/1 Error 0 1h
train-76-f993c8d8 0/1 Completed 0 1h
webserver-7fc9c69f4d-mnrjj 2/2 Running 0 36m
worker-6997bf76bd-kvjx4 2/2 Running 0 25m
worker-6997bf76bd-prxbg 2/2 Running 0 36m


and I'd like to get rid of the pods like train-204-d3f2337c. How can I do that?










share|improve this question






















  • I had similar task but my duplicate pods are created by kubernetes job. in that case you only need deleted the job and the pods automatically deleted.

    – Lei Yang
    Mar 9 at 1:44













0












0








0








Answer my own question



I have a bunch of pods in kubernetes which are completed (successfully or unsuccessfully) and I'd like to clean up the output of kubectl get pods. Here's what I see when I run kubectl get pods:



NAME READY STATUS RESTARTS AGE
intent-insights-aws-org-73-ingest-391c9384 0/1 ImagePullBackOff 0 8d
intent-postgres-f6dfcddcc-5qwl7 1/1 Running 0 23h
redis-scheduler-dev-master-0 1/1 Running 0 10h
redis-scheduler-dev-metrics-85b45bbcc7-ch24g 1/1 Running 0 6d
redis-scheduler-dev-slave-74c7cbb557-dmvfg 1/1 Running 0 10h
redis-scheduler-dev-slave-74c7cbb557-jhqwx 1/1 Running 0 5d
scheduler-5f48b845b6-d5p4s 2/2 Running 0 36m
snapshot-169-5af87b54 0/1 Completed 0 20m
snapshot-169-8705f77c 0/1 Completed 0 1h
snapshot-169-be6f4774 0/1 Completed 0 1h
snapshot-169-ce9a8946 0/1 Completed 0 1h
snapshot-169-d3099b06 0/1 ImagePullBackOff 0 24m
snapshot-204-50714c88 0/1 Completed 0 21m
snapshot-204-7c86df5a 0/1 Completed 0 1h
snapshot-204-87f35e36 0/1 ImagePullBackOff 0 26m
snapshot-204-b3a4c292 0/1 Completed 0 1h
snapshot-204-c3d90db6 0/1 Completed 0 1h
snapshot-245-3c9a7226 0/1 ImagePullBackOff 0 28m
snapshot-245-45a907a0 0/1 Completed 0 21m
snapshot-245-71911b06 0/1 Completed 0 1h
snapshot-245-a8f5dd5e 0/1 Completed 0 1h
snapshot-245-b9132236 0/1 Completed 0 1h
snapshot-76-1e515338 0/1 Completed 0 22m
snapshot-76-4a7d9a30 0/1 Completed 0 1h
snapshot-76-9e168c9e 0/1 Completed 0 1h
snapshot-76-ae510372 0/1 Completed 0 1h
snapshot-76-f166eb18 0/1 ImagePullBackOff 0 30m
train-169-65f88cec 0/1 Error 0 20m
train-169-9c92f72a 0/1 Error 0 1h
train-169-c935fc84 0/1 Error 0 1h
train-169-d9593f80 0/1 Error 0 1h
train-204-70729e42 0/1 Error 0 20m
train-204-9203be3e 0/1 Error 0 1h
train-204-d3f2337c 0/1 Error 0 1h
train-204-e41a3e88 0/1 Error 0 1h
train-245-7b65d1f2 0/1 Error 0 19m
train-245-a7510d5a 0/1 Error 0 1h
train-245-debf763e 0/1 Error 0 1h
train-245-eec1908e 0/1 Error 0 1h
train-76-86381784 0/1 Completed 0 19m
train-76-b1fdc202 0/1 Error 0 1h
train-76-e972af06 0/1 Error 0 1h
train-76-f993c8d8 0/1 Completed 0 1h
webserver-7fc9c69f4d-mnrjj 2/2 Running 0 36m
worker-6997bf76bd-kvjx4 2/2 Running 0 25m
worker-6997bf76bd-prxbg 2/2 Running 0 36m


and I'd like to get rid of the pods like train-204-d3f2337c. How can I do that?










share|improve this question














Answer my own question



I have a bunch of pods in kubernetes which are completed (successfully or unsuccessfully) and I'd like to clean up the output of kubectl get pods. Here's what I see when I run kubectl get pods:



NAME READY STATUS RESTARTS AGE
intent-insights-aws-org-73-ingest-391c9384 0/1 ImagePullBackOff 0 8d
intent-postgres-f6dfcddcc-5qwl7 1/1 Running 0 23h
redis-scheduler-dev-master-0 1/1 Running 0 10h
redis-scheduler-dev-metrics-85b45bbcc7-ch24g 1/1 Running 0 6d
redis-scheduler-dev-slave-74c7cbb557-dmvfg 1/1 Running 0 10h
redis-scheduler-dev-slave-74c7cbb557-jhqwx 1/1 Running 0 5d
scheduler-5f48b845b6-d5p4s 2/2 Running 0 36m
snapshot-169-5af87b54 0/1 Completed 0 20m
snapshot-169-8705f77c 0/1 Completed 0 1h
snapshot-169-be6f4774 0/1 Completed 0 1h
snapshot-169-ce9a8946 0/1 Completed 0 1h
snapshot-169-d3099b06 0/1 ImagePullBackOff 0 24m
snapshot-204-50714c88 0/1 Completed 0 21m
snapshot-204-7c86df5a 0/1 Completed 0 1h
snapshot-204-87f35e36 0/1 ImagePullBackOff 0 26m
snapshot-204-b3a4c292 0/1 Completed 0 1h
snapshot-204-c3d90db6 0/1 Completed 0 1h
snapshot-245-3c9a7226 0/1 ImagePullBackOff 0 28m
snapshot-245-45a907a0 0/1 Completed 0 21m
snapshot-245-71911b06 0/1 Completed 0 1h
snapshot-245-a8f5dd5e 0/1 Completed 0 1h
snapshot-245-b9132236 0/1 Completed 0 1h
snapshot-76-1e515338 0/1 Completed 0 22m
snapshot-76-4a7d9a30 0/1 Completed 0 1h
snapshot-76-9e168c9e 0/1 Completed 0 1h
snapshot-76-ae510372 0/1 Completed 0 1h
snapshot-76-f166eb18 0/1 ImagePullBackOff 0 30m
train-169-65f88cec 0/1 Error 0 20m
train-169-9c92f72a 0/1 Error 0 1h
train-169-c935fc84 0/1 Error 0 1h
train-169-d9593f80 0/1 Error 0 1h
train-204-70729e42 0/1 Error 0 20m
train-204-9203be3e 0/1 Error 0 1h
train-204-d3f2337c 0/1 Error 0 1h
train-204-e41a3e88 0/1 Error 0 1h
train-245-7b65d1f2 0/1 Error 0 19m
train-245-a7510d5a 0/1 Error 0 1h
train-245-debf763e 0/1 Error 0 1h
train-245-eec1908e 0/1 Error 0 1h
train-76-86381784 0/1 Completed 0 19m
train-76-b1fdc202 0/1 Error 0 1h
train-76-e972af06 0/1 Error 0 1h
train-76-f993c8d8 0/1 Completed 0 1h
webserver-7fc9c69f4d-mnrjj 2/2 Running 0 36m
worker-6997bf76bd-kvjx4 2/2 Running 0 25m
worker-6997bf76bd-prxbg 2/2 Running 0 36m


and I'd like to get rid of the pods like train-204-d3f2337c. How can I do that?







bash kubernetes






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 8 at 23:06









Paymahn MoghadasianPaymahn Moghadasian

1,86842541




1,86842541












  • I had similar task but my duplicate pods are created by kubernetes job. in that case you only need deleted the job and the pods automatically deleted.

    – Lei Yang
    Mar 9 at 1:44

















  • I had similar task but my duplicate pods are created by kubernetes job. in that case you only need deleted the job and the pods automatically deleted.

    – Lei Yang
    Mar 9 at 1:44
















I had similar task but my duplicate pods are created by kubernetes job. in that case you only need deleted the job and the pods automatically deleted.

– Lei Yang
Mar 9 at 1:44





I had similar task but my duplicate pods are created by kubernetes job. in that case you only need deleted the job and the pods automatically deleted.

– Lei Yang
Mar 9 at 1:44












2 Answers
2






active

oldest

votes


















1














If this pods created by CronJob, you can use spec.failedJobsHistoryLimit and spec.successfulJobsHistoryLimit



Example:



apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: my-cron-job
spec:
schedule: "*/10 * * * *"
failedJobsHistoryLimit: 1
successfulJobsHistoryLimit: 3
jobTemplate:
spec:
template:
...





share|improve this answer






























    0














    Here's a one liner which will delete all pods which aren't in the Running or Pending state (note that if a pod name has Running or Pending in it, it won't get deleted ever with this one liner):



    kubectl get pods --no-headers=true |grep -v "Running" | grep -v "Pending" | sed -E 's/([a-z0-9-]+).*/1/g' | xargs kubectl delete pod



    Here's an explanation:



    1. get all pods without any of the headers

    2. filter out pods which are Running

    3. filter out pods which are Pending

    4. pull out the name of the pod using a sed regex

    5. use xargs to delete each of the pods by name

    Note, this doesn't account for all pod states. For example, if a pod is in the state ContainerCreating this one liner will delete that pod too.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55072235%2fhow-to-delete-completed-kubernetes-pod%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      If this pods created by CronJob, you can use spec.failedJobsHistoryLimit and spec.successfulJobsHistoryLimit



      Example:



      apiVersion: batch/v1beta1
      kind: CronJob
      metadata:
      name: my-cron-job
      spec:
      schedule: "*/10 * * * *"
      failedJobsHistoryLimit: 1
      successfulJobsHistoryLimit: 3
      jobTemplate:
      spec:
      template:
      ...





      share|improve this answer



























        1














        If this pods created by CronJob, you can use spec.failedJobsHistoryLimit and spec.successfulJobsHistoryLimit



        Example:



        apiVersion: batch/v1beta1
        kind: CronJob
        metadata:
        name: my-cron-job
        spec:
        schedule: "*/10 * * * *"
        failedJobsHistoryLimit: 1
        successfulJobsHistoryLimit: 3
        jobTemplate:
        spec:
        template:
        ...





        share|improve this answer

























          1












          1








          1







          If this pods created by CronJob, you can use spec.failedJobsHistoryLimit and spec.successfulJobsHistoryLimit



          Example:



          apiVersion: batch/v1beta1
          kind: CronJob
          metadata:
          name: my-cron-job
          spec:
          schedule: "*/10 * * * *"
          failedJobsHistoryLimit: 1
          successfulJobsHistoryLimit: 3
          jobTemplate:
          spec:
          template:
          ...





          share|improve this answer













          If this pods created by CronJob, you can use spec.failedJobsHistoryLimit and spec.successfulJobsHistoryLimit



          Example:



          apiVersion: batch/v1beta1
          kind: CronJob
          metadata:
          name: my-cron-job
          spec:
          schedule: "*/10 * * * *"
          failedJobsHistoryLimit: 1
          successfulJobsHistoryLimit: 3
          jobTemplate:
          spec:
          template:
          ...






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 9 at 9:13









          ArslanbekovArslanbekov

          633419




          633419























              0














              Here's a one liner which will delete all pods which aren't in the Running or Pending state (note that if a pod name has Running or Pending in it, it won't get deleted ever with this one liner):



              kubectl get pods --no-headers=true |grep -v "Running" | grep -v "Pending" | sed -E 's/([a-z0-9-]+).*/1/g' | xargs kubectl delete pod



              Here's an explanation:



              1. get all pods without any of the headers

              2. filter out pods which are Running

              3. filter out pods which are Pending

              4. pull out the name of the pod using a sed regex

              5. use xargs to delete each of the pods by name

              Note, this doesn't account for all pod states. For example, if a pod is in the state ContainerCreating this one liner will delete that pod too.






              share|improve this answer



























                0














                Here's a one liner which will delete all pods which aren't in the Running or Pending state (note that if a pod name has Running or Pending in it, it won't get deleted ever with this one liner):



                kubectl get pods --no-headers=true |grep -v "Running" | grep -v "Pending" | sed -E 's/([a-z0-9-]+).*/1/g' | xargs kubectl delete pod



                Here's an explanation:



                1. get all pods without any of the headers

                2. filter out pods which are Running

                3. filter out pods which are Pending

                4. pull out the name of the pod using a sed regex

                5. use xargs to delete each of the pods by name

                Note, this doesn't account for all pod states. For example, if a pod is in the state ContainerCreating this one liner will delete that pod too.






                share|improve this answer

























                  0












                  0








                  0







                  Here's a one liner which will delete all pods which aren't in the Running or Pending state (note that if a pod name has Running or Pending in it, it won't get deleted ever with this one liner):



                  kubectl get pods --no-headers=true |grep -v "Running" | grep -v "Pending" | sed -E 's/([a-z0-9-]+).*/1/g' | xargs kubectl delete pod



                  Here's an explanation:



                  1. get all pods without any of the headers

                  2. filter out pods which are Running

                  3. filter out pods which are Pending

                  4. pull out the name of the pod using a sed regex

                  5. use xargs to delete each of the pods by name

                  Note, this doesn't account for all pod states. For example, if a pod is in the state ContainerCreating this one liner will delete that pod too.






                  share|improve this answer













                  Here's a one liner which will delete all pods which aren't in the Running or Pending state (note that if a pod name has Running or Pending in it, it won't get deleted ever with this one liner):



                  kubectl get pods --no-headers=true |grep -v "Running" | grep -v "Pending" | sed -E 's/([a-z0-9-]+).*/1/g' | xargs kubectl delete pod



                  Here's an explanation:



                  1. get all pods without any of the headers

                  2. filter out pods which are Running

                  3. filter out pods which are Pending

                  4. pull out the name of the pod using a sed regex

                  5. use xargs to delete each of the pods by name

                  Note, this doesn't account for all pod states. For example, if a pod is in the state ContainerCreating this one liner will delete that pod too.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 8 at 23:06









                  Paymahn MoghadasianPaymahn Moghadasian

                  1,86842541




                  1,86842541



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55072235%2fhow-to-delete-completed-kubernetes-pod%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      AWS Lex not identifying response if by a variable The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceEnforcing custom enumeration in AWS LEX for slot valuesHow to give response based on user response in Amazon Lex?Intercepting AWS Lambda Response to a AWS Lex QueryLex chat bot error: Reached second execution of fulfillment lambda on the same utteranceamazon lex showing invalid responseLambda response send back to Lex slot?Response card in Amazon lexAmazon Lex - Lambda response return HTML to botHow can I solve 424 (Failed Dependency) (python) obtained from Amazon lex?

                      Алба-Юлія

                      Захаров Федір Захарович