Jenkins: remove old builds with command line

Jenkins

Jenkins Problem Overview


I delete old jenkins builds with rm where job is hosted:

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

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

Jenkins Solutions


Solution 1 - Jenkins

Here is another option: delete the builds remotely with cURL. (Replace the beginning of the URLs with whatever you use to access Jenkins with your browser.)

$ 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: As pointed out by yegeniy in a comment below, one might have to replace doDeleteAll by doDelete in the URLs above to make this work, depending on the configuration.

Solution 2 - Jenkins

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

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

Solution 3 - Jenkins

  1. Check your home jenkins directory:
  • "Manage Jenkins" ==> "Configure System" enter image description here
  • Check field "Home directory" (usually it is /var/lib/jenkins)

Command for delete all jenkins job builds

/jenkins_home/jobs> rm -rf */builds/*

2. After delete should reload config:

* **"Manage Jenkins" ==> "Reload Configuration from Disk"**

enter image description here

Solution 4 - Jenkins

You can do it by Groovy Scripts using Hudson API.. Access your jenkins instalation

http://localhost:38080/script.

For Example, for deleting all old builds of all projects using the follow script: Note: Take care if you use Finger Prints , you will lose all history.

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 for cleaning all workspaces :

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 a lot of examples on the Jenkins wiki

Solution 5 - Jenkins

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

You can change your job configuration to automatically delete old builds, based either on number of days or number of builds. No more worrying about it or having to keep track, Jenkins just does it for you.

Solution 6 - Jenkins

The following script cleans old builds of jobs. You should reload config from disk if you delete build 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")
    }
}

Solution 7 - Jenkins

From Jenkins Scriptler console run the following Groovy script to delete all the builds of jobs listed under a 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()
        }

    }
}

Solution 8 - Jenkins

From 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()

Solution 9 - Jenkins

def jobName = "MY_JOB_NAME"
def job = Jenkins.instance.getItem(jobName)
job.getBuilds().findAll { it.number < 10 }.each { it.delete() }

if you had 12 builds this would clear out builds 0-9 and you'd have 12,11,10 remaining. Just drop in the script console

Solution 10 - Jenkins

This script will configure the build retention settings of all of the Jenkins jobs.

Change the values from 30 and 200 to suite you needs, run the script, then restart the Jenkins service.

#!/bin/bash

cd $HOME
for xml in $(find jobs -name config.xml)
do
	sed -i 's#<daysToKeep>.*#<daysToKeep>30</daysToKeep>#' $xml
	sed -i 's#<numToKeep>.*#<numToKeep>200</numToKeep>#' $xml
done

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPhilippe BlayoView Question on Stackoverflow
Solution 1 - JenkinsErwan LegrandView Answer on Stackoverflow
Solution 2 - JenkinsCIGuyView Answer on Stackoverflow
Solution 3 - JenkinsqxoView Answer on Stackoverflow
Solution 4 - JenkinsEduardo FabricioView Answer on Stackoverflow
Solution 5 - JenkinsDennis S.View Answer on Stackoverflow
Solution 6 - Jenkinshello-worldView Answer on Stackoverflow
Solution 7 - JenkinsManojView Answer on Stackoverflow
Solution 8 - JenkinsNael MarwanView Answer on Stackoverflow
Solution 9 - JenkinsJeffCharterView Answer on Stackoverflow
Solution 10 - JenkinsAAber View Answer on Stackoverflow