1

I'm attempting to delete the build history from Jenkins using the instructions in this answer:

How do I clear my Jenkins/Hudson build history?

However, I'm getting the following error message:

groovy.lang.MissingMethodException: No signature of method: jenkins.branch.OrganizationFolder.getBuilds() is applicable for argument types: () values: [] Possible solutions: getViews(), doBuild(jenkins.util.TimeDuration), getUrl(), getClass(), getActions(), getApi() at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:58) at org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:49) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)

If I run this script:

def jobName = "github-test"
def job = Jenkins.instance.getItem(jobName)
println(job)

I get the following output:

jenkins.branch.OrganizationFolder@134f3a3c[github-test]

I'm using version 2.32.2.

Here's the exact script that I ran:

def jobName = "github-test"
def job = Jenkins.instance.getItem(jobName)
job.getBuilds().each { it.delete() }
job.nextBuildNumber = 1
job.save()
Community
  • 1
  • 1
opike
  • 7,053
  • 14
  • 68
  • 95

2 Answers2

5

The job that you are trying to delete, github-test, is an organization folder, e.g. you get jenkins.branch.OrganizationFolder. This means that it's not an actual job with actual builds, instead it's a folder which contains other jobs. Worth noting is that OrganizationFolder is of the type ComputedFolder which means that it is populated automatically by Jenkins when it finds new repositories and branches (on github).

So I'm guessing that you wan't to remove all builds from the sub-jobs in your github-test job/folder. I've modified the linked answer so that it accounts for folders:

import com.cloudbees.hudson.plugins.folder.AbstractFolder
import hudson.model.AbstractItem

// change this variable to match the name of the job whose builds you want to delete
def jobName = "test"
// Set to true in order to reset build number to 1
def resetBuildNumber = false

def removeBuilds(job, resetBuildNumber) {
  if (job instanceof AbstractFolder) {
    for (subJob in job.getItems()) {
      removeBuilds(subJob, resetBuildNumber)
    }
  } else if (job instanceof AbstractItem) {
    job.getBuilds().each { it.delete() }
    if (resetBuildNumber) {
      job.nextBuildNumber = 1
      job.save()
    }
  } else {
    throw new RuntimeException("Unsupported job type ${job.getClass().getName()}!")
  }
}
removeBuilds(Jenkins.instance.getItem(jobName), resetBuildNumber)
Jon S
  • 15,846
  • 4
  • 44
  • 45
2

You can use the script below to delete builds. You can specify how many latest builds to retain, just add a value of your choice to MAX_BUILDS.make it 0 to delete everything.

Assuming the builds you want to deleted belongs to "github-test". Replace jobName value with your choice

MAX_BUILDS = 5
def jobName = "github-test"
def job = Jenkins.instance.getItem(jobName)

println ""

println "selected Jenkins Job : "
println job.name

def recent = job.builds.limit(MAX_BUILDS)
println recent

  for (build in job.builds) {
    if (!recent.contains(build)) {
      println ""
      println "========================================================="
      println "Preparing to delete: " + build
      build.delete()
    println ""
    }
  }
Pang
  • 9,564
  • 146
  • 81
  • 122
prudviraj
  • 3,634
  • 2
  • 16
  • 22
  • If I have multiple jobs, how should I delete them. for example: def jobs = ["World-2.6.0.6", "Hello-1.7.0"] – binbjz Sep 06 '17 at 04:08
  • Add your list of jobs into an array, iterate over the array in a for loop and reuse the other part of deleting builds inside the for loop. – prudviraj Sep 06 '17 at 13:44