Answers: <p>We can't do it by freestyle or manually but we can achieve this by writing a pipeline script for all the jobs in one and running it.</p>
  <p><em><strong>Example code</strong></em>:</p>
  <pre><code>pipeline {
    agent any
    stages {
      stage('Disable discard old builds on all jobs') {
        steps {
          // Get all jobs
          def jobs = Jenkins.instance.getAllItems(Job.class)
  
          // Iterate over all jobs and disable the &quot;Discard old builds&quot; option
          jobs.each { job -&gt;
            // Get the job configuration
            def jobConfig = job.getConfigFile()
  
            // Disable the &quot;Discard old builds&quot; option
            jobConfig.properties.remove(jobConfig.properties.find { it instanceof BuildDiscarderProperty })
  
            // Update the job configuration
            jobConfig.save()
          }
        }
      }
    }
  }
  </code></pre>
  <p>Mention all the jobs accordingly...
  &amp; The pipeline job will iterate over all jobs in Jenkins and disable the &quot;Discard old builds&quot; option on each job.</p>
  
Answers: CC BY-SA 4.0
Answers: 2023-11-02T19:46:18.893Z
Answers: Almost. I get the error `somePath/someJob
  groovy.lang.MissingMethodException: No signature of method: hudson.XmlFile.save() is applicable for argument types: () values: []
  Possible solutions: wait(), any(), wait(long), each(groovy.lang.Closure), any(groovy.lang.Closure), isCase(java.lang.Object)`
Answers: Chris F
Answers: CC BY-SA 4.0
Answers: 2023-11-02T19:49:21.663Z
Answers: If I change `jobConfig.save()` to `job.save()` I don't get the error, but I don't see the configuration change.
Answers: Chris F
Answers: CC BY-SA 4.0
Answers: 2023-11-03T15:32:01.180Z
Answers: I posted the question here also, and someone posted a solution. https://stackoverflow.com/questions/77411857/in-jenkins-how-can-i-disable-a-jobs-discard-old-builds-via-pipeline-code/77415259#77415259
Answers: Chris F
Answers: CC BY-SA 4.0
Answers: 2023-11-02T19:21:38.083Z
Answers: 2023-11-02T19:21:38.083Z
Answers: AV Reddy
Body: <p>I have several jobs where I want to disable the option shown below in the jobs' configuration page. How can I do it via pipeline?</p>
  <p><a href="https://i.sstatic.net/6WNsq.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/6WNsq.png" alt="Jenkins job config page" /></a></p>
  
Tags: <jenkins><jenkins-pipeline><groovy>
Title: In Jenkins, how can I disable "Discard old builds" option via pipeline?
UserDisplayName: Chris F
ThreadText: # In Jenkins, how can I disable "Discard old builds" option via pipeline?
  
  (Asked by: username_0 on 2023-11-02)
  
  I have several jobs where I want to disable the option shown below in the jobs' configuration page. How can I do it via pipeline?
  
  [![Jenkins job config page](https://i.sstatic.net/6WNsq.png)](https://i.sstatic.net/6WNsq.png)
  
  **Tags:** jenkins, jenkins-pipeline, groovy
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-11-02. Score: 2
  
  We can't do it by freestyle or manually but we can achieve this by writing a pipeline script for all the jobs in one and running it.
  
  ***Example code***:
  
  ```
  pipeline {
    agent any
    stages {
      stage('Disable discard old builds on all jobs') {
        steps {
          // Get all jobs
          def jobs = Jenkins.instance.getAllItems(Job.class)
  
          // Iterate over all jobs and disable the "Discard old builds" option
          jobs.each { job ->
            // Get the job configuration
            def jobConfig = job.getConfigFile()
  
            // Disable the "Discard old builds" option
            jobConfig.properties.remove(jobConfig.properties.find { it instanceof BuildDiscarderProperty })
  
            // Update the job configuration
            jobConfig.save()
          }
        }
      }
    }
  }
  
  ```
  
  Mention all the jobs accordingly...
  & The pipeline job will iterate over all jobs in Jenkins and disable the "Discard old builds" option on each job.
  
  > **Comments:**
  > **username_0**: Almost. I get the error `somePath/someJob groovy.lang.MissingMethodException: No signature of method: hudson.XmlFile.save() is applicable for argument types: () values: [] Possible solutions: wait(), any(), wait(long), each(groovy.lang.Closure), any(groovy.lang.Closure), isCase(java.lang.Object)`  
  > **username_0**: If I change `jobConfig.save()` to `job.save()` I don't get the error, but I don't see the configuration change.  
  > **username_0**: I posted the question here also, and someone posted a solution. https://stackoverflow.com/questions/77411857/in-jenkins-how-can-i-disable-a-jobs-discard-old-builds-via-pipeline-code/77415259#77415259  
  
  ---
  
  