Deleting builds from a job but not the promoted ones

Article ID:216694687
1 minute readKnowledge base

Issue

You want to delete all builds from a job, but not the ones that were promoted.

Resolution

The following script run in the Jenkins script console at Manage Jenkins » Script Console does that. Note that you should always make sure to have good backups before deleting things like this in a script.

Jenkins.instance.getItemByFullName("yourJobNameHere").builds.each {
  def p = it.getAction(hudson.plugins.promoted_builds.PromotedBuildAction)
  if (!p || !p.hasPromotion()) {
    it.delete()
  }
}
return

Replace "yourJobNameHere" with the name of the job.

This article is part of our Knowledge Base and is provided for guidance-based purposes only. The solutions or workarounds described here are not officially supported by CloudBees and may not be applicable in all environments. Use at your own discretion, and test changes in a safe environment before applying them to production systems.