Automatically Marking Jenkins builds as keep forever

Article ID:218762207
1 minute readKnowledge base


  • Builds which are promoted to stable environments (e.g., Production) are automatically purged according to the configured Jenkins old build purge schedule


To manually prevent a build from getting purged according to the schedule, click on the 'Keep this build forever' button to lock the build.

There are different ways to do it automatically based on the type of the Project.

In the case of Pipeline, the property is exposed on the Build object. Therefore, you can, for example, keep the current build forever by adding the following in your Pipeline:

currentBuild.keepLog = true

To do this in other types of projects, you can leverage the Groovy plugin’s build step "Execute system Groovy script" to execute the "keepLog" function on the build that has to be kept forever.

For example, to mark the current build:

println "Marking current build as Keep Forever"

To mark any arbitrary project’s build:

// Get the Project/Job object
def job = jenkins.model.Jenkins.instance.getItemByFullName("folder/test-discard-old-builds", Job.class)
// Get the build by number
def myBuild = job.getBuild("4")
// Keep this build forever