Skip to content
Latchkey

How to Set Build Retention in Jenkins

buildDiscarder caps how many builds and artifacts Jenkins keeps on disk.

Declare buildDiscarder with logRotator in the options block. Set how many builds to keep and, separately, how many to keep artifacts for, to control storage.

Retain a bounded build history

Keep the last 20 builds but artifacts for only the last 5.

Jenkinsfile
pipeline {
  agent any
  options {
    buildDiscarder(logRotator(
      numToKeepStr: '20',
      artifactNumToKeepStr: '5'
    ))
  }
  stages {
    stage('Build') {
      steps { sh 'make build' }
    }
  }
}

Notes

  • artifactNumToKeepStr prunes heavy artifacts faster than full build records, saving the most disk.
  • Use daysToKeepStr instead of counts if you want a time-based policy.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →