Clear Jenkins build history ( clear build yesterday )


I need to clear and clean Jenkins build history: Yesterday2 days agoAll builds. How can I do it in Jenkins?

Best Answer

  • Delete a Jenkins build via GUI.

    Go into the build you want to delete, and click the  Delete this build   button in the upper right corner.

    enter image description here

  • If you need to clean the Jenkins build history, and reset the build number back to 1, you can run a simple script in Jenkins Script Console. source

    1. Go to Jenkins Script Console.

      Go to your Jenkins home page -> Manage Jenkins -> Script Console.

      enter image description here

    2. Run this script to clean and reset.

      Copy and paste this script to your Console Script text area and change the "copy_folder" to the project name that you need to clean the history. Then click the Run button.

      def jobName = "copy_folder"  
      def job = Jenkins.instance.getItem(jobName)  
      job.getBuilds().each { it.delete() }  
      job.nextBuildNumber = 1

      If you're using multibranch pipeline or folders, the second line of the above script has to be modified like this:

      def jobName = "path/to/your/jenkins/pipeline/master"
      def job = Jenkins.getInstance().getItemByFullName(jobName, Job.class)

Another way to remove old builds or jobs is from the command-line.

Login to Jenkins command-line.

To remove an entire job and build history remove the job folder, e.g.:

rm -rf jobs/jobname

To look at build history:

$ ls jobs/jobname/builds

1409  1411  1413  1415  1417  1419  1421  1423  1425
923  963  974  985  lastFailedBuild  lastSuccessfulBuild  
lastUnsuccessfulBuild 1191  1334  1348  1379  1381  1383
1406  1408  1410  1412  1414  1416  1418  1420  1422  1424
913   962  973  978  987  lastStableBuild  lastUnstableBuild

To remove selected builds then remove the build number directory, e.g.:

rm -rf jobs/jobname/builds/97* jobs/jobname/builds/13*

Don't forget the last step if removing or editing files under Jenkins.

After removing jobs or builds from file system you need to go to Manage Jenkins in the Jenkins GUI and click Reload Configuration from Disk.

Note: The Jenkins home directory might be in /var/jenkins_home or /var/lib/jenkins or another location.