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)

  • Jenkins How can i upload a text file and use it as a parameter
  • Jenkins, parameterized builds and the ability to select the required subprojects and child nodes based on variables
  • Jenkins Git userContent plugin
  • How to run jenkins as a different user
  • Why is Jenkins failing when fetching from git, while the command line isn't?
  • Configure default permissions for newly registered Jenkins users
  • Why is Jenkins ignoring the %PATH% variable when using MSBuild?
  • jenkins on the same virtual machine as critical application, is it wise?
  • Jenkins operational log
  • Trigger a jenkins build from git using cloudbees and bitbucket
  • Maven/Jenkins java.lang.UnsupportedClassVersionError: Unsupported major.minor version 51.0
  • XCodeBuild -exportArchive wont allow me to specify filename
  • 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.