101

I am using Jenkins and Gradle to build my java project.

Every time I build my project, I get a new build number on the Jenkins screen.

The following is my Jenkins build info:

Success > Console Output  #96   03-Jan-2014 15:35:08    
Success > Console Output  #95   03-Jan-2014 15:27:29     
Failed > Console Output  #94    03-Jan-2014 15:26:16     
Failed > Console Output  #93    03-Jan-2014 15:25:01     
Failed > Console Output  #92    03-Jan-2014 15:23:50     
Success > Console Output  #91   03-Jan-2014 12:42:32    
Success > Console Output  #90   03-Jan-2014 12:02:45

I want to reset the Jenkins build number like:

Success > Console Output  #1    03-Jan-2014 12:02:45

How can I reset the build number in Jenkins?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Sivakumar M
  • 1,567
  • 4
  • 18
  • 22

14 Answers14

178

Can be easier done from groovy script console . Go to http://your-jenkins-server/script In script window enter:

item = Jenkins.instance.getItemByFullName("your-job-name-here")
//THIS WILL REMOVE ALL BUILD HISTORY
item.builds.each() { build ->
  build.delete()
}
item.updateNextBuildNumber(1)
antweiss
  • 2,789
  • 1
  • 13
  • 12
  • 8
    or via cli `java -jar jenkins-cli.jar -s https://jenkins.example.com groovysh \ 'jenkins.instance.getItemByFullName("job-name").updateNextBuildNumber(1021)'` – ykhrustalev Jan 31 '16 at 17:24
  • 3
    This is definitely the best answer. You dont even need the delete loop if you only want to set the number. – Nick Mar 16 '16 at 12:04
  • For some reason this only performs the delete on the latest build, however the following did work for me: `echo "deleting all builds of ${jobName}" item = Jenkins.instance.getItemByFullName("${jobName}") for (build in item.builds){ build.delete(); } item.updateNextBuildNumber(1)` – BRNTZN Dec 22 '16 at 15:14
  • A noob question.. how i know exact job-name. – Ammar Mujeeb Jul 06 '17 at 08:18
  • @AmmarMujeeb go to your Jenkins dashboard and you'll see the job name there. You can also go to `/var/lib/jenkins/jobs/` on Linux and the name of the directory there is the exact job name – rickrizzo Aug 17 '17 at 15:14
  • I've posted this as an answer in its own right, but since it probably won't make it to the top of the heap ... the `build` object has a `number` property, upon which you can condition, so you can delete just a partial segment of the build history, to rollback past a few builds you want to delete. – Brondahl Mar 01 '18 at 16:06
  • 1
    This worked except running build afterwards fails and produces errors in log: WARNING: A new build could not be created in job java.lang.IllegalStateException: JENKINS-27530: cannot create a build with number 1 since that (or higher) is already in use among [xxx, xxx, xxx, ... ] – Geyser14 Jan 15 '19 at 22:58
  • It is possible to store the script in Jenkins -> Scriptler, including parameter and description for later re-use. This requires to add import line in the script: import jenkins.model.Jenkins ... – LiborStefek Jun 03 '20 at 06:57
  • For jobs in folders: item = Jenkins.instance.getItemByFullName("folder/jobname") – mms Jul 17 '22 at 09:38
64

From here

Given your Hudson job is named FooBar,

  • rename FooBar to FooBar-Copy
  • create a new job named FooBar, using 'Copy existing job' option, from FooBar-Copy
  • delete FooBar-Copy
MokaT
  • 1,416
  • 16
  • 37
37
  • First wipeout workspace and get rid of previous builds.
    • On the server navigate to the job dir eg. 'var/lib/jenkins/jobs/myJob' delete the workspace & build dirs as well as any polling files, lastSuccessful, lastStable files etc. You should only have config.xml and lastBuildNumber.
    • Shut down jenkins using something like service jenkins stop
    • Edit the file called nextBuildNumber, inserting 1 instead of the current build number
    • Start up jenkins again, service jenkins start
    • Log into jenkins and go to your job and hit build. Should start building job#1
jasonoriordan
  • 873
  • 16
  • 25
24

If you want set the next build number, there is plugin "NextBuildNumber" for that. But this will not work in your case because the build number you need, which is 1, is lesser than your current build number.

Here need to wipe out all the previous builds first. You can do this by running this simple script Go to -> Manage Jenkins -> Script console

// change this variable to match the name of the job whose builds you want to delete
def jobName = "Your Job Name"
def job = Jenkins.instance.getItem(jobName)
job.getBuilds().each { it.delete() }

Now you can set next build number to 1 and run the build. It will start with 1. :) Its that simple.

Update - Jenkins now has a Purge Job History plugin to get this done in easiest way. Checkout the page for more details - https://wiki.jenkins.io/display/JENKINS/Purge+Job+History+Plugin

Amol Manthalkar
  • 1,890
  • 2
  • 16
  • 16
6

To more generally reset your build number to N (where N is not necessarily 1):

  • Delete any existing builds where buildNumber >= N.
  • Edit Program Files (x86)/Jenkins/jobs/yourjob/nextBuildNumber. Set the number it contains to N.
  • From Jenkins, select Manage Jenkins -> Reload Configuration from Disk.
c32hedge
  • 785
  • 10
  • 19
  • Make sure you read this defect before Selecting "Reload configuration from disk" -- https://issues.jenkins-ci.org/browse/JENKINS-32984 – user3181500 Jul 17 '17 at 13:22
6

Expanding on the accepted answer, here's how to do it for all projects at once:

Jenkins.instance.allItems.each() { 
  item -> item.builds.each() { 
    build -> build.delete()
  }
  item.updateNextBuildNumber(1)
}  
zb226
  • 9,586
  • 6
  • 49
  • 79
6

As an extention of @antweiss's excellent answer, we can actually go further ...

There's no need to delete the full Build History, if you don't want to, you can simply roll back time, to a prior point:

resetNumberTarget = 14
item = Jenkins.instance.getItemByFullName("Project Name [from project Dashboard]")
//println(item)

item.builds.each() { build ->
  //println(build)
  //println(build.number)

  if(build.number >= resetNumberTarget)
  {
    //println("About to Delete '" + build + "'")    
    build.delete()  
  }
}

item.updateNextBuildNumber(resetNumberTarget)

If you want a dummy run, to check what it's going to do, without actually doing it, simply comment out the build.delete() and item.updateNextBuildNumber(resetNumberTarget) lines and uncomment the various print commands.


Documentation: Details of these objects were hard to find, but I identified the following:
Brondahl
  • 7,402
  • 5
  • 45
  • 74
2

Use Purge Job History plugin (Jenkins >= 2.176.1)

https://plugins.jenkins.io/purge-job-history/

dizarter
  • 448
  • 1
  • 8
  • 21
1

You can use either nexBuildNumber plug-in or simply modify nexBuildNumber file to reset build number. Following are the steps that you need to perform:

  • Go to .jenkins/Jobs/<YourJobName>/build/, take backup of this folder(if you need for future use) and delete build folder.

Note: Once you clean up old builds, you lose build histories and they are no longer available on the Jenkins dashboard.

  • Reload the configuration(Jenkins -> Manage Jenkins).
  • Set next build version to 1 by either using the Next Build Number plug-in or modifying the nextBuildNumber file in yourjob directory.
Anni S
  • 1,996
  • 19
  • 28
1

So I tried the above solution and getting the following error.,

groovy.lang.MissingPropertyException: No such property: builds for class: org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject.

So I tried this,

item = Jenkins.get().getItem("Job Name")
jobs = item.getAllJobs()
jobs.each() { item -> 
  builds = item.getBuilds()
  builds.each() { b -> 
    b.delete()
  }
  item.updateNextBuildNumber(1)

}

and it worked!!

Sam
  • 4,046
  • 8
  • 31
  • 47
0

Here is variation of @antweiss answer for multi-branch pipeline

items = Jenkins.instance.getItemByFullName("your-job-name-here").getItems()
//THIS WILL REMOVE ALL BUILD HISTORY
items.collectMany { it.builds }.each { build ->
  build.delete()
}
items.each {
  it.updateNextBuildNumber(1)
}
guiliguili
  • 11
  • 1
0

Jenkins script(Test on Jenkins 2.387.3):

def jobName = "<YOUR_PIPELINE_NAME>"
def job = Jenkins.instance.getItem(jobName)
job.getBuilds().each { it.delete() }
job.updateNextBuildNumber(1)

Delete all build history and reset build number.

Crystal
  • 101
  • 1
  • 1
  • 5
-1

To reset build numbers of all jobs:

Jenkins.instance.getAllItems(AbstractProject.class).each {
    item = Jenkins.instance.getItemByFullName(it.fullName)
    //THIS WILL REMOVE ALL BUILD HISTORY
    item.builds.each() { build ->
        build.delete()
    }
    item.updateNextBuildNumber(1)
}
GRK
  • 1
-1

I found an easy way to do this.

  1. Wipe out your work space.
  2. Go to each builds which saved on Jenkins and delete it.
  3. Set build number to 1 then build.

Jenkins use previous build to determine the next build number, if build number you input is lower than previous build number, Jenkins will automatically increase your build number to higher than previous build. So here we just

Mai Quân Nguyễn
  • 369
  • 1
  • 3
  • 5