[jenkins] How do I clear my Jenkins/Hudson build history?

I recently updated the configuration of one of my hudson builds. The build history is out of sync. Is there a way to clear my build history?

Please and thank you

The answer is


Navigate to: %JENKINS_HOME%\jobs\jobName

Open the file "nextBuildNumber" and change the number. After that reload Jenkins configuration. Note: "nextBuildNumber" file contains the next build no that will be used by Jenkins.


Go to the %HUDSON_HOME%\jobs\<projectname> remove builds dir and remove lastStable, lastSuccessful links, and remove nextBuildNumber file.

After doing above steps go to below link from UI
Jenkins-> Manage Jenkins -> Reload Configuration from Disk

It will do as you need


Another easy way to clean builds is by adding the Discard Old Plugin at the end of your jobs. Set a maximum number of builds to save and then run the job again:

https://wiki.jenkins-ci.org/display/JENKINS/Discard+Old+Build+plugin


You could modify the project configuration temporarily to save only the last 1 build, reload the configuration (which should trash the old builds), then change the configuration setting again to your desired value.


If you want to clear the build history of MultiBranchProject (e.g. pipeline), go to your Jenkins home page ? Manage Jenkins ? Script Console and run the following script:

def projectName = "ProjectName"
def project = Jenkins.instance.getItem(projectName)
def jobs = project.getItems().each {
  def job = it
  job.getBuilds().each { it.delete() }
  job.nextBuildNumber = 1
  job.save()
}

Here is another option: delete the builds with cURL.

$ curl -X POST http://jenkins-host.tld:8080/jenkins/job/myJob/[1-56]/doDeleteAll

The above deletes build #1 to #56 for job myJob.

If authentication is enabled on the Jenkins instance, a user name and API token must be provided like this:

$ curl -u userName:apiToken -X POST http://jenkins-host.tld:8080/jenkins/job/myJob/[1-56]/doDeleteAll

The API token must be fetched from the /me/configure page in Jenkins. Just click on the "Show API Token..." button to display both the user name and the API token.

Edit: one might have to replace doDeleteAll by doDelete in the URLs above to make this work, depending on the configuration or the version of Jenkins used.


This one is the best option available.

Jenkins.instance.getAllItems(AbstractProject.class).each {it -> Jenkins.instance.getItemByFullName(it.fullName).builds.findAll { it.number > 0 }.each { it.delete() } }

This code will delete all Jenkins Job build history.


Use the script console (Manage Jenkins > Script Console) and something like this script to bulk delete a job's build history https://github.com/jenkinsci/jenkins-scripts/blob/master/scriptler/bulkDeleteBuilds.groovy

That script assumes you want to only delete a range of builds. To delete all builds for a given job, use this (tested):

// change this variable to match the name of the job whose builds you want to delete
def jobName = "Your Job Name"
def job = Jenkins.instance.getItem(jobName)

job.getBuilds().each { it.delete() }
// uncomment these lines to reset the build number to 1:
//job.nextBuildNumber = 1
//job.save()

Here is how to delete ALL BUILDS FOR ALL JOBS...... using the Jenkins Scripting.

def jobs = Jenkins.instance.projects.collect { it } 
jobs.each { job -> job.getBuilds().each { it.delete() }} 

Go to "Manage Jenkins" > "Script Console"

Run below:

def jobName = "build_name"  
def job = Jenkins.instance.getItem(jobName)  
job.getBuilds().each { it.delete() }  
job.save()

If using the Script Console method then try using the following instead to take into account if jobs are being grouped into folder containers.

def jobName = "Your Job Name"
def job = Jenkins.instance.getItemByFullName(jobName)

or

def jobName = "My Folder/Your Job Name
def job = Jenkins.instance.getItemByFullName(jobName)

Deleting directly from file system is not safe. You can run the below script to delete all builds from all jobs ( recursively ).

def numberOfBuildsToKeep = 10
Jenkins.instance.getAllItems(AbstractItem.class).each {
  if( it.class.toString() != "class com.cloudbees.hudson.plugins.folder.Folder" && it.class.toString() != "class org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject") {
    println it.name
    builds = it.getBuilds()
    for(int i = numberOfBuildsToKeep; i < builds.size(); i++) {
        builds.get(i).delete()
      println "Deleted" + builds.get(i)
    }
  }
}

This answer is for Jenkins

  1. Go to your Jenkins home page ? Manage Jenkins ? Script Console

    Enter image description here

  2. Run the following script there. Change copy_folder to your project name

Code:

def jobName = "copy_folder"
def job = Jenkins.instance.getItem(jobName)
job.getBuilds().each { it.delete() }
job.nextBuildNumber = 1
job.save()

My post


Using Script Console.

In case the jobs are grouped it's possible to either give it a full name with forward slashes:

getItemByFullName("folder_name/job_name") 
job.getBuilds().each { it.delete() }
job.nextBuildNumber = 1
job.save()

or traverse the hierarchy like this:

def folder = Jenkins.instance.getItem("folder_name")
def job = folder.getItem("job_name")
job.getBuilds().each { it.delete() }
job.nextBuildNumber = 1
job.save()

Examples related to jenkins

Maven dependencies are failing with a 501 error Jenkins pipeline how to change to another folder Docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock groovy.lang.MissingPropertyException: No such property: jenkins for class: groovy.lang.Binding How to solve npm install throwing fsevents warning on non-MAC OS? Run bash command on jenkins pipeline Try-catch block in Jenkins pipeline script How to print a Groovy variable in Jenkins? Jenkins pipeline if else not working Error "The input device is not a TTY"

Examples related to continuous-integration

Jenkins CI Pipeline Scripts not permitted to use method groovy.lang.GroovyObject Jenkins vs Travis-CI. Which one would you use for a Open Source project? Continuous Integration vs. Continuous Delivery vs. Continuous Deployment How do I clone a job in Jenkins? How to trigger Jenkins builds remotely and to pass parameters How to connect Bitbucket to Jenkins properly Managing SSH keys within Jenkins for Git How to uninstall Jenkins? How to set environment variables in Jenkins? Jenkins CI: How to trigger builds on SVN commit

Examples related to hudson

How/When does Execute Shell mark a build as failure in Jenkins? How to create and add users to a group in Jenkins for authentication? In Jenkins, how to checkout a project into a specific directory (using GIT) Jenkins - passing variables between jobs? How are environment variables used in Jenkins with Windows Batch Command? Jenkins/Hudson - accessing the current build number? Error - trustAnchors parameter must be non-empty Archive the artifacts in Jenkins Jenkins / Hudson environment variables How to trigger a build only if changes happen on particular set of files

Examples related to build-automation

What is Gradle in Android Studio? Maven: Non-resolvable parent POM Controlling Maven final name of jar artifact How do I clear my Jenkins/Hudson build history? How can I get CMake to find my alternative Boost installation? Ant: How to execute a command for each file in directory? Xcode variables Why does Maven have such a bad rep? How can I create an executable JAR with dependencies using Maven? Ant task to run an Ant target only if a file exists?