50
votes

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)

10
Not an answer: but useful, if you have groovy : github.com/jenkinsci/jenkins-scripts/blob/master/scriptler/…Jayan

10 Answers

42
votes

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

55
votes

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.

24
votes
  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/*
  1. After delete should reload config:

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

enter image description here

12
votes

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

9
votes

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.

1
votes

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")
    }
}
0
votes

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

    }
}
0
votes

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()
0
votes
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

0
votes

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