Disk space management in Jenkins - Time & Space Complexity
When managing disk space in Jenkins, it is important to understand how the time to clean or check disk usage grows as the amount of data increases.
We want to know how the time needed changes when there are more files or builds to handle.
Analyze the time complexity of the following Jenkins pipeline script that deletes old build artifacts to free disk space.
pipeline {
agent any
stages {
stage('Clean Disk') {
steps {
script {
def builds = currentBuild.rawBuild.project.builds
builds.each { build ->
if (build.getTimeInMillis() < System.currentTimeMillis() - 30L*24*60*60*1000) {
build.delete()
}
}
}
}
}
}
}
This script loops through all builds and deletes those older than 30 days to manage disk space.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through all builds in the project.
- How many times: Once for each build stored in Jenkins.
The time to check and delete builds grows as the number of builds increases.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks and possible deletions |
| 100 | 100 checks and possible deletions |
| 1000 | 1000 checks and possible deletions |
Pattern observation: The work grows directly with the number of builds; doubling builds doubles the work.
Time Complexity: O(n)
This means the time needed grows in a straight line with the number of builds to check.
[X] Wrong: "Deleting old builds takes the same time no matter how many builds there are."
[OK] Correct: Each build must be checked, so more builds mean more work and more time.
Understanding how disk cleanup time grows helps you design Jenkins jobs that scale well and keep the system healthy.
"What if we only checked the last 50 builds instead of all builds? How would the time complexity change?"