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
How to Use the input Step for Approval in JenkinsPause a Jenkins declarative pipeline for human approval using the input directive so a deploy stage waits for…
How to Run on a Kubernetes Agent in JenkinsRun Jenkins pipeline stages in ephemeral Kubernetes pods using the kubernetes plugin and an inline podTemplat…