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?"
Practice
Solution
Step 1: Understand Jenkins build process
Jenkins stores build files and logs in workspaces which consume disk space.Step 2: Identify the effect of disk space on builds
If disk space runs out, builds can fail or Jenkins may slow down.Final Answer:
To keep Jenkins builds running smoothly without storage issues -> Option DQuick Check:
Disk space management = smooth builds [OK]
- Thinking disk space affects only UI speed
- Confusing disk space with user management
- Assuming plugins control disk space
Solution
Step 1: Recall Jenkins pipeline steps for workspace cleanup
The stepcleanWs()is designed to delete all files in the current workspace.Step 2: Verify other options
deleteDir()deletes the current directory but is less specific; others are not valid Jenkins steps.Final Answer:
cleanWs() -> Option BQuick Check:
Workspace cleanup step = cleanWs() [OK]
- Using deleteDir() which deletes current dir but not workspace safely
- Assuming removeWorkspace() or clearDisk() exist
- Confusing cleanWs() with plugin names
cleanWs() at the end of a Jenkins pipeline script?Solution
Step 1: Understand cleanWs() behavior
cleanWs() deletes all files in the workspace where the build runs.Step 2: Consider placement at pipeline end
Placing cleanWs() at the end deletes files after build finishes, freeing disk space.Final Answer:
The workspace files will be deleted after the build completes -> Option CQuick Check:
cleanWs() at end = workspace cleaned [OK]
- Thinking cleanWs() backs up files
- Assuming it causes build failure
- Believing cleanWs() does nothing
cleanWs() in your Jenkins pipeline, but disk space is still filling up. What is a likely cause?Solution
Step 1: Understand cleanWs() limitations
cleanWs() deletes workspace files but does not manage other disk usage like old builds or logs.Step 2: Role of Disk Usage Plugin
The Disk Usage Plugin helps monitor and automate cleanup beyond workspace, including old builds and artifacts.Final Answer:
The Disk Usage Plugin is not installed or configured -> Option AQuick Check:
Disk Usage Plugin needed for full cleanup [OK]
- Believing cleanWs() deletes all disk files
- Thinking cleanWs() deletes only old files
- Assuming cleanWs() needs manual approval
Solution
Step 1: Use Disk Usage Plugin for monitoring and cleanup
This plugin tracks disk usage and can be set to delete old builds automatically.Step 2: Use cleanWs() in pipelines for workspace cleanup
Adding cleanWs() deletes workspace files after builds, freeing space regularly.Final Answer:
Configure the Disk Usage Plugin and add cleanWs() in pipelines -> Option AQuick Check:
Plugin + cleanWs() = automated cleanup [OK]
- Relying on manual deletion only
- Disabling plugins instead of cleaning
- Ignoring disk size limits
Ready to prove it?
Pass the test and this pattern moves unknown
