Artifact retention policies in Jenkins - Time & Space Complexity
When Jenkins keeps build artifacts, it uses rules to decide how many to keep.
We want to know how the time to clean old artifacts grows as the number of builds increases.
Analyze the time complexity of the following Jenkins pipeline snippet that deletes old artifacts.
pipeline {
agent any
options {
buildDiscarder(logRotator(numToKeepStr: '10'))
}
stages {
stage('Build') {
steps {
echo 'Building...'
}
}
}
}
This code keeps only the latest 10 builds' artifacts and deletes older ones automatically.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Jenkins scans all previous builds to find which artifacts to delete.
- How many times: It checks each build once to decide if it is older than the limit.
As the number of builds grows, Jenkins must check more builds to delete old artifacts.
| Input Size (n builds) | Approx. Operations (checks) |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of checks grows directly with the number of builds.
Time Complexity: O(n)
This means the time to clean old artifacts grows in a straight line as the number of builds increases.
[X] Wrong: "Deleting old artifacts happens instantly no matter how many builds exist."
[OK] Correct: Jenkins must check each build to decide if its artifacts should be deleted, so more builds mean more work.
Understanding how Jenkins handles artifact cleanup helps you explain how build systems manage storage efficiently as projects grow.
"What if the retention policy kept artifacts based on time (e.g., last 30 days) instead of number of builds? How would the time complexity change?"
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
