What if your Jenkins server could clean up after itself and never run out of space?
Why Artifact retention policies in Jenkins? - Purpose & Use Cases
Imagine you run a busy bakery where every day you bake dozens of cakes and store them all in the fridge. Over time, the fridge gets so full that you can't find the fresh cakes easily, and old cakes start to spoil.
In Jenkins, this is like keeping every build artifact forever without cleaning up.
Manually deleting old build files is slow and risky. You might delete something important by mistake or forget to clean up, causing your storage to fill up and Jenkins to slow down or crash.
This wastes time and can break your automation pipeline.
Artifact retention policies automatically keep only the important build files and delete the rest after a set time or number of builds.
This keeps your storage clean, Jenkins fast, and your builds reliable without manual effort.
Keep all build artifacts forever Manually delete files when storage is full
Set 'Discard old builds' in Jenkins Define days or number of builds to keep artifacts
Automatically managing build files so your Jenkins server stays clean, fast, and efficient without manual work.
A software team runs nightly builds and stores test reports and binaries. With artifact retention policies, only the last 10 builds' files are kept, saving disk space and avoiding slowdowns.
Manual cleanup of build files is slow and error-prone.
Artifact retention policies automate cleanup based on rules.
This keeps Jenkins storage healthy and builds running smoothly.
Practice
Solution
Step 1: Understand artifact retention policies
Artifact retention policies control how long build files are kept to avoid using too much disk space.Step 2: Identify the main goal
The main goal is to delete old build files to free up disk space.Final Answer:
To manage disk space by deleting old build files -> Option AQuick Check:
Artifact retention = disk space management [OK]
- Confusing retention with build triggering
- Thinking retention speeds up builds
- Assuming retention keeps all builds forever
Solution
Step 1: Identify correct syntax for retention
Retention usesbuildDiscarderwithlogRotatorto specify how many builds to keep.Step 2: Check option correctness
buildDiscarder(logRotator(numToKeepStr: '5')) uses correct syntax:buildDiscarder(logRotator(numToKeepStr: '5')). Others misusearchiveArtifactsor wrong parameters.Final Answer:
buildDiscarder(logRotator(numToKeepStr: '5')) -> Option CQuick Check:
Retention uses buildDiscarder + logRotator [OK]
- Using archiveArtifacts to set retention
- Wrong parameter names like keepLast
- Nesting logRotator inside archiveArtifacts
pipeline {
options {
buildDiscarder(logRotator(numToKeepStr: '3', daysToKeepStr: '7'))
}
stages {
stage('Build') {
steps {
echo 'Building...'
}
}
}
}
What happens to builds older than 7 days or beyond the last 3 builds?Solution
Step 1: Understand logRotator parameters
It keeps only the last 3 builds and deletes builds older than 7 days.Step 2: Determine effect on old builds
Builds older than 7 days or beyond the last 3 are deleted to save space.Final Answer:
They are deleted to free disk space -> Option AQuick Check:
logRotator deletes old builds [OK]
- Thinking builds are archived permanently
- Assuming builds trigger new builds
- Believing builds are kept forever
options {
buildDiscarder(logRotator(artifactDaysToKeep: '10'))
}
But artifacts are not deleted after 10 days. What is the likely problem?Solution
Step 1: Check logRotator parameter names
logRotator parameters for strings end with 'Str', like artifactDaysToKeepStr.Step 2: Identify the syntax issue
Missing 'Str' means the parameter is not recognized, so default retention applies and artifacts aren't deleted after 10 days.Final Answer:
The parameter name should be artifactDaysToKeepStr, not artifactDaysToKeep -> Option DQuick Check:
logRotator params end with 'Str' [OK]
- Using wrong parameter names
- Expecting logRotator to delete artifacts
- Placing buildDiscarder outside options block
Solution
Step 1: Understand roles for artifact-specific retention
To keep artifacts from only the last 4 builds while retaining builds, useartifactNumToKeepStr.Step 2: Check option correctness
options { buildDiscarder(logRotator(artifactNumToKeepStr: '4')) }:buildDiscarder(logRotator(artifactNumToKeepStr: '4'))keeps artifacts from last 4 builds, deletes older artifacts, keeps all builds. C deletes old builds too. B/D use invalid params.Final Answer:
options { buildDiscarder(logRotator(artifactNumToKeepStr: '4')) } -> Option BQuick Check:
artifactNumToKeepStr for artifacts only [OK]
- Using invalid parameters like retentionDays or keepOnlyLast
- Trying to control retention only via archiveArtifacts
- Misplacing buildDiscarder outside options block
Ready to prove it?
Pass the test and this pattern moves unknown
