Jenkins: remove old builds with command line

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.


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


  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


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

Tags:

Jenkins