How to delete old Jenkins builds on repository branches

Jenkins

I have used the Blue Ocean plugin to create a Jenkins job that automatically uses the Jenkinsfile (declarative pipeline) to build and report the status back to GitHub. That all works fine.

The issue I have is that I cannot get old builds deleted. The job shows as a repository and I have configured it to keep a maximum of 5 builds. But I noticed that the branches indexed, including the master branch, also have their own configuration. I tried setting up the old builds deletion but there’s no save button! I thought this was because the configuration propagates from the repository to the branches, but it has no effect. How can I get this to work?

Best Answer

Certain job properties are adjusted using directives in Jenkinsfiles and not the configuration in the web UI. In your Jenkinsfile:

properties([
  // only keep 25 builds to prevent disk usage from growing out of control
  buildDiscarder(
    logRotator(
      artifactDaysToKeepStr: '', 
      artifactNumToKeepStr: '', 
      daysToKeepStr: '', 
      numToKeepStr: '25',
    ),
  ),
])

You may of course adjust the various parameters to suit your needs.