Jenkins: remove old builds with command line

I remove the old jenkins builds with rm where the job is located:

 my_job/builds/$ rm -rf [1-9]* 

These old builds are still visible on the job page. How to remove them using the command line?
(no delete button in each build user interface)

+46
jenkins
Oct 24
source share
10 answers

It looks like this has been added to the CLI or is at least being processed: http://jenkins.361315.n4.nabble.com/How-to-purge-old-builds-td385290.html

The syntax would be something like this: java -jar jenkins-cli.jar -s http://my.jenkins.host delete-builds myproject '1-7499' --username $user --password $password

+41
Oct 24 '12 at 15:57
source share

Here is another option: delete assemblies remotely using cURL. (Replace the beginning of the URL with what you use to access Jenkins with your browser.)

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

The above removes build # 1 - # 56 for job myJob.

If authentication is enabled on the Jenkins instance, you must specify the username and API token as follows:

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

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

Edit: as indicated by yegeniy in the comment below, you may have to replace doDeleteAll with doDelete in the URLs above to do this work depending on your configuration.

+52
Mar 10 '14 at 14:41
source share
  1. Check your Jenkins home directory:
    • "Managing Jenkins" ==> "Configure System" enter image description here
    • Check the Home Directory field (usually / var / lib / jenkins )

Command to delete all jenkins job assemblies

 /jenkins_home/jobs> rm -rf */builds/* 
  1. After removal, you should reload the config:

    • "Managing Jenkins" ==> "Reload configuration from disk"

enter image description here

+24
Jun 05 '13 at 3:48
source share

You can do this using Groovy Scripts using the Hudson API .. Access to jenkins installation

 http://localhost:38080/script. 

For example, to delete all old collections of all projects using script: Note. Be careful if you use Finger Prints, you will lose the whole story.

 import hudson.model.* // For each project for(item in Hudson.instance.items) { // check that job is not building if(!item.isBuilding()) { System.out.println("Deleting all builds of job "+item.name) for(build in item.getBuilds()){ build.delete() } } else { System.out.println("Skipping job "+item.name+", currently building") } } 

Or to clean all work areas:

 import hudson.model.* // For each project for(item in Hudson.instance.items) { // check that job is not building if(!item.isBuilding()) { println("Wiping out workspace of job "+item.name) item.doDoWipeOutWorkspace() } else { println("Skipping job "+item.name+", currently building") } } 

There are many examples on the Jenkins wiki

+10
Aug 11 '14 at 18:26
source share

Is there a reason you need to do this manually instead of letting Jenkins remove old builds for you?

You can change the configuration of your work to automatically delete old assemblies based on either the number of days or the number of assemblies. No more worrying about it or tracking, Jenkins just does it for you.

+7
Oct. 25 '12 at 17:34
source share

The following script clears old job assemblies. You must reload the configuration from disk if you delete the assembly manually:

 import hudson.model.* for(item in Hudson.instance.items) { if (!item.isBuilding()) { println("Deleting old builds of job " + item.name) for (build in item.getBuilds()) { //delete all except the last if (build.getNumber() < item.getLastBuild().getNumber()) { println "delete " + build try { build.delete() } catch (Exception e) { println e } } } } else { println("Skipping job " + item.name + ", currently building") } } 
+1
Feb 05 '17 at 10:20
source share

In the Jenkins Scriptler console, run the following Groovy script to remove all job assemblies listed in the view:

 import jenkins.model.Jenkins hudson.model.Hudson.instance.getView('<ViewName>').items.each() { println it.fullDisplayName def jobname = it.fullDisplayName def item = hudson.model.Hudson.instance.getItem(jobname) def build = item.getLastBuild() if (item.getLastBuild() != null) { Jenkins.instance.getItemByFullName(jobname).builds.findAll { it.number <= build.getNumber() }.each { it.delete() } } } 
0
Nov 03 '16 at 5:28
source share

From the script console Run this, but you need to change the job name:

 def jobName = "name" def job = Jenkins.instance.getItem(jobName) job.getBuilds().each { it.delete() } job.nextBuildNumber = 1 job.save() 
0
Apr 21 '19 at 10:16
source share
 def jobName = "MY_JOB_NAME" def job = Jenkins.instance.getItem(jobName) job.getBuilds().findAll { it.number < 10 }.each { it.delete() } 

if you had 12 assemblies, that would clear assemblies 0-9, and you would have 12,11,10 left. Just look into the script console

0
Jul 26 '19 at 18:14
source share

Go to the job settings panel in Jenkins, find Source Code Management-> Additional Behaviors-> Click "Add", select "Clear" before checking. Jenkins will clean old builds on this build

-5
Nov 14 '14 at 6:27
source share



All Articles