Jenkins: remove old builds with command line

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)

  • Restore snapshot before build in jenkins using virtualbox plugin
  • Webservice call from Jenkins
  • Unable to see selenium tests are running on the browser when the test is run by Jenkins
  • Using an arbitrary plugin in Jenins 2 Pipeline scripts
  • Jenkins and github
  • How can I know how long a Jenkins job has been in the wait queue after the job is finished?
  • creating jenkins jobs with ansible
  • having trouble during initial build for project “gameoflife” when following “jenkins the definitive guide” by john ferguson
  • hudson/jenkins keeps building the bad branch in git instead of the good one
  • how to create pull request if jenkins job running on feature branch is successful
  • ant/Jenkins: move doesn't support the “quiet” attribute
  • How to exclude git branch from building in Jenkins
  • 8 Solutions collect form web for “Jenkins: remove old builds with command line”

    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

    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.

    Command for delete all jenkins job builds

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

    after delete should reload config:

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

    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

    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.

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

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

    Go to configure panel of the job in Jenkins, find Source Code Management->Additional Behaviors->Click Add, choose Clean before checkout. The Jenkins will clean the old builds on this build

    Git Baby is a git and github fan, let's start git clone.