How do I clear my Jenkins/Hudson build history?
Asked Answered
S

17

92

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

Swivel answered 4/8, 2010 at 21:51 Comment(5)
Can you elaborate on "totally messed up"? Maybe someone will have experienced a similar issue before and can direct you on how to fix it.Macassar
sorry about the ambiguity. fixed.Swivel
There is a newer and slightly more specific question here: #13052890Alake
This post may help codeketchup.blogspot.sg/2016/05/…Sender
Here is the best answer I found: superuser.com/questions/1418885/…Bore
S
51

If you click Manage Hudson / Reload Configuration From Disk, Hudson will reload all the build history data.

If the data on disk is messed up, you'll need to go to your %HUDSON_HOME%\jobs\<projectname> directory and restore the build directories as they're supposed to be. Then reload config data.

If you're simply asking how to remove all build history, you can just delete the builds one by one via the UI if there are just a few, or go to the %HUDSON_HOME%\jobs\<projectname> directory and delete all the subdirectories there -- they correspond to the builds. Afterwards restart the service for the changes to take effect.

Saintmihiel answered 4/8, 2010 at 21:57 Comment(4)
I have deleted all files and folders inside "/var/lib/jenkins/jobs/Demo" but I when go to Jenkins UI inside this JOB I still see builds history.Phillip
After you "monkey" with the directory structure, just go to "Manage Jenkins" and select "Reload Configuration From Disk"Inkwell
@paul, after delete the folders there, a restart of Jenkins is needed to see the build history disappeared from the UI. BTW, if we're using Jenkins, the path is typically located at /var/jenkins_home/jobs/{project name}/builds/Cowshed
I think this solution requires to much steps on the system side, a much more clean solution is provided in the answer from @Nayana AdassuriyaSignificance
M
119

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()
Midwifery answered 31/7, 2013 at 7:32 Comment(7)
great trick, but how about resetting the build counter? any scripting for that?Hoenack
@JesChergui I've updated to show resetting the build number. The script console lets you control all sorts of things, see javadoc.jenkins-ci.org (AbstractProject is a good place to start for playing with jobs)Midwifery
My Jenkins complained that it wasn't allowed to access instances, or such like. I was running it from a new pipeline I'd setup specifically for this deletion and I pasted in the code above and changed the name.Heeltap
@Heeltap this is due to the script security plugin which puts pipeline jobs in a super restrictive sandbox. See if you have a signature to approve under "Manage Jenkins" > "In-process script approvals". Another option would be to use the Scriptler plugin's ability to run admin scripts in a job: wiki.jenkins-ci.org/display/JENKINS/Scriptler+PluginMidwifery
I am getting this error ; java.lang.NullPointerException: Cannot invoke method getBuilds() on null objectFrump
@muhammedozbilici try using Jenkins.instance.getItemByFullName instead of Jenkins.instance.getItemKings
@Michael Platings actually, it was about script console security, you have to approve these methods.Frump
S
84

This answer is for Jenkins

  1. Go to your Jenkins home page → Manage JenkinsScript 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

Sender answered 28/6, 2016 at 6:36 Comment(4)
Best answer so farProtolanguage
This is the fastest solution.Marymarya
Yeah, but for this one, DON'T restart Jenkins or it doesn't seem to update the nextbuildnumber to 1 when you do a build after the restart. Mine started at 39 so I had to redo this.Heeltap
Working as of Jenkins 2.75Histrionic
S
51

If you click Manage Hudson / Reload Configuration From Disk, Hudson will reload all the build history data.

If the data on disk is messed up, you'll need to go to your %HUDSON_HOME%\jobs\<projectname> directory and restore the build directories as they're supposed to be. Then reload config data.

If you're simply asking how to remove all build history, you can just delete the builds one by one via the UI if there are just a few, or go to the %HUDSON_HOME%\jobs\<projectname> directory and delete all the subdirectories there -- they correspond to the builds. Afterwards restart the service for the changes to take effect.

Saintmihiel answered 4/8, 2010 at 21:57 Comment(4)
I have deleted all files and folders inside "/var/lib/jenkins/jobs/Demo" but I when go to Jenkins UI inside this JOB I still see builds history.Phillip
After you "monkey" with the directory structure, just go to "Manage Jenkins" and select "Reload Configuration From Disk"Inkwell
@paul, after delete the folders there, a restart of Jenkins is needed to see the build history disappeared from the UI. BTW, if we're using Jenkins, the path is typically located at /var/jenkins_home/jobs/{project name}/builds/Cowshed
I think this solution requires to much steps on the system side, a much more clean solution is provided in the answer from @Nayana AdassuriyaSignificance
A
24

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.

Alake answered 10/3, 2014 at 14:40 Comment(1)
Super useful, thank you! This is the only answer (on this question anyway) which allows for selective deletion, for instance when you discover a job that wasn't previously configured to discard old builds and want to get rid of a lot (but not all) of history.Berna
T
12

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() }} 
Togliatti answered 5/5, 2015 at 19:32 Comment(1)
jenkins 2.293 returned : Result: [] and build were not removedSendai
D
9

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.

Deen answered 4/8, 2010 at 22:16 Comment(2)
Sorry, haven't used Hudson now for a couple of years, but see this about reloading the configuration: #5217052Deen
As someone who does not have admin permissions to Jenkins, this was the only option that worked for me.Grimbly
S
8

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.

Stodgy answered 3/9, 2018 at 8:7 Comment(2)
For anyone wondering how to run the above code, go to Jenkins Home > Manage Jenkins > Script Console. There will be a text field there that you can enter this script into.Stradivari
jenkins 2.293 returned the job names but builds were not removedSendai
N
7

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()
}
Neopythagoreanism answered 15/8, 2018 at 10:38 Comment(1)
This is the only that worked. Thanks Marin!Extraditable
T
3

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()
Toponym answered 8/6, 2018 at 16:30 Comment(0)
H
2

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)
    }
  }
}
Hydrolysate answered 19/7, 2019 at 12:52 Comment(0)
F
2

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()
Finisterre answered 22/2, 2021 at 13:45 Comment(0)
P
1

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

Pyrognostics answered 3/3, 2015 at 13:11 Comment(0)
P
1

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

Perfervid answered 3/5, 2016 at 7:51 Comment(0)
P
1

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)
Poncho answered 24/4, 2017 at 15:8 Comment(0)
T
0

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.

Truthful answered 8/10, 2014 at 19:53 Comment(0)
S
0

Tested on jenkins 2.293 over linux. It will remove all the build logs but not the corellative build number

cd /var/lib/jenkins/jobs
find . -name "builds" -exec rm -rf {} \;

Be careful with this command because it executes a rm -rf on each find result. You could exec this first to validate if the result are only the builds folder of you jobs

find . -name "builds"
Sendai answered 15/7, 2022 at 0:55 Comment(0)
H
0

If you are looking for a solution where you have job inside a Folder you can use getItemByFullName function. It also supports white space in folder and job name.

def jobName = "folder_name/job_name"
def job = Jenkins.instance.getItemByFullName(jobName)
job.getBuilds().each { it.delete() }
job.nextBuildNumber = 1
job.save()
Helping answered 15/2, 2023 at 11:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.