30

I need to clear and clean Jenkins build history: Yesterday - 2 days ago - All builds. How can I do it in Jenkins?

1
  • A more complete answer is here, including CLI, API and UI. It's bad practice to go under the covers and clean via bash; could leave app in indeterminate config. Suggest deleting your Q as duplicate. Also, since LTS 2.204.6: Add globally configured build discarders that delete old builds not marked as "keep forever" even if there is no, or a less aggressive, per-project build discarder configured, executed periodically and after a build finishes. (pull 4368)
    – Ian W
    Nov 21, 2020 at 4:50

4 Answers 4

41
  • 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   
      job.save()
      

      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
legacyIds

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.

4
  • I need delete All project . How can i put text into replay "copy_folder" ? Mar 29, 2019 at 6:42
  • 1
    If you don't have too many projects you can replace "copy_folder" with the text of each project, one project at a time until you have deleted all projects. There is also a Discard Old Build Jenkins plugin that hasn't been updated since 2014 at GitHub.
    – karel
    Mar 29, 2019 at 6:50
  • @karel we use discard old builds plugin, it's very useful, it allows us to have long running jenkins jobs without manually removing or scripting up something custom to remove old builds.
    – gaoithe
    Apr 4, 2019 at 12:22
  • And another way to remove old builds or jobs: on the command-line remove the numbered build directories or remove the full job directory then in manage jenkins do "Reload Configuration from Disk". I will edit and add this to end of answer.
    – gaoithe
    Apr 4, 2019 at 12:24
2

The explanation above is awesome. It covers the different ways of cleaning up the history, jobs, etc.

Just an idea building on the above: wrap the command-line instructions for removal with a Jenkins job and parameterize the build numbers, e.g. specify a CSV in a job parameter and parse it in the shell script associated with the job to remove the targeted builds. And/or add a DELETE_ALL_BUILDS checkbox parameter and check it with and IF statement in the script - if selected, remove all builds. You can also parameterize the name of the job, e.g. provide a parameter called JOB_NAME, and interpolate the value in the script, e.g.:

rm -rf jobs/${JOB_NAME}/builds/${BUILD_NUM}*
0

You must be a Jenkins administrator to do this task.  Below is example of Jenkins job 305.

  • Open Jenkins → select the branch you need to be deleted → select Build History
  • Build History option → Select red circle of the error build (#305)
        Build History option
  • Jenkins job error → Select Delete Build:
        Delete Build option
  • Confirmation message before deleting → Select Yes to delete.
        Confirmation message before deleting
0
# cd to your Jenkins install directory with the userid that has "permissions"

sudo bash     ( if you have to )

# My installation is like this, so go wherever you placed yours:

cd /reps/jenkins/home/jobs/TestPlaywrightSuite/builds 

# Touch these two files to exclude from the auto search below
#   but since these are regular files, they should be excluded from
#   the find command anyway
touch permalinks
touch legacyids

# find directories older than 7 days and delete on one command

find . -type d -mtime +7 -exec rm -rf {} \;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .