Delete the Jenkins Build History for all jobs using Groovy Script
If you maintain the all Jenkins jobs related build history means it will occupy the more disk space on a server. To avoid such cases, we need to clean the build history.
for example: As per my requirement, I need to keep only latest 5 build history-related information of each Jenkins Job. For this requirement, I created one job and scheduling this to trigger weekly once.It will maintain latest 5 build history related logs only for all jobs.
Delete the build history info with help of Groovy script:
def buildLogCount=5Jenkins.instance.getAllItems(Job.class).each { jobitem ->
def jobName = jobitem.name
def jobInfo = Jenkins.instance.getItem(jobName)
if(jobInfo.getLastBuild()){
def lastBuildNum=jobInfo.getLastBuild().getNumber()
if(lastBuildNum>=buildLogCount){
def deletedBuildNum=lastBuildNum - buildLogCount
def deletedNumRange = Fingerprint.RangeSet.fromString("0-${deletedBuildNum}",false);
def buildCount=jobInfo.getBuilds(deletedNumRange).size()
if(buildCount==0){
println "No build logs found in ${jobName} and build history count is : ${buildCount}"
}
else{
jobInfo.getBuilds(deletedNumRange).each { item ->
item.delete()
}
}
}
else{
println "No build logs to delete in ${jobName}"
}
}
else{
println "No build logs found in ${jobName}"
}
}
Note: We can customize the above script in smart way according to our requirement. It’s only practice(i.e. Test) script. Even it will work only stand alone jobs.
For more detailed way, please visit below link:
== If you find any thing wrong please let me know i will update my self ==