Skip to content

Instantly share code, notes, and snippets.

@danilodeveloper
Last active September 8, 2016 01:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danilodeveloper/425e83c734c1476e2865 to your computer and use it in GitHub Desktop.
Save danilodeveloper/425e83c734c1476e2865 to your computer and use it in GitHub Desktop.
Groovy script - Jenkins - How to keep the last 4 builds and delete the rest
hudson.model.Hudson.instance.items.findAll{job -> job.isBuildable() && job.name.contains("stb")}.each{
job ->
println("Job : ${job.name}");
builds = jenkins.model.Jenkins.instance.getItem(job.name).getBuilds().reverse();
for(i = 0; i < builds.size() - 4; i++){
builds[i].delete();
println(" Build - ${builds[i]} - Deleted With Success.");
}
println(" Deleted Builds - ${builds.size()}");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment