Bird
Raised Fist0
Jenkinsdevops~5 mins

Disk space management in Jenkins - Commands & Configuration

Choose your learning style10 modes available
Introduction
Disk space management in Jenkins helps keep the server clean and running smoothly by removing old build files and logs that take up too much space.
When Jenkins server disk is filling up and builds start failing due to lack of space
When you want to automatically delete old build artifacts to save storage
When you want to keep only recent build logs for troubleshooting and remove older ones
When you want to prevent Jenkins from slowing down because of too many stored files
When you want to schedule cleanup tasks to run regularly without manual intervention
Config File - Jenkinsfile
Jenkinsfile
pipeline {
    agent any
    options {
        buildDiscarder(logRotator(numToKeepStr: '10', artifactNumToKeepStr: '5'))
    }
    stages {
        stage('Build') {
            steps {
                echo 'Building the project'
            }
        }
    }
}

This Jenkinsfile defines a pipeline with a buildDiscarder option that automatically deletes old builds and artifacts.

numToKeepStr: '10' means keep only the last 10 builds.

artifactNumToKeepStr: '5' means keep artifacts only for the last 5 builds.

This helps manage disk space by cleaning up old data.

Commands
This command lists all Jenkins jobs on the server to identify which jobs exist before applying disk space management.
Terminal
java -jar jenkins-cli.jar -s http://localhost:8080 list-jobs
Expected OutputExpected
example-job build-app test-pipeline
This command downloads the configuration XML of the 'example-job' to edit disk space settings if needed.
Terminal
java -jar jenkins-cli.jar -s http://localhost:8080 get-job example-job > example-job.xml
Expected OutputExpected
<?xml version='1.0' encoding='UTF-8'?> <project> <actions/> <description>Example job</description> <keepDependencies>false</keepDependencies> <properties/> <scm class="hudson.scm.NullSCM"/> <canRoam>true</canRoam> <disabled>false</disabled> <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding> <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding> <triggers/> <concurrentBuild>false</concurrentBuild> <builders/> <publishers/> <buildWrappers/> </project>
This command uploads the edited job configuration XML back to Jenkins to apply disk space management settings.
Terminal
java -jar jenkins-cli.jar -s http://localhost:8080 update-job example-job < example-job.xml
Expected OutputExpected
No output (command runs silently)
This command lists builds of the 'example-job' to verify which builds exist before and after cleanup.
Terminal
java -jar jenkins-cli.jar -s http://localhost:8080 list-builds example-job
Expected OutputExpected
#1 #2 #3 #4 #5 #6 #7 #8 #9 #10 #11 #12
Key Concept

If you remember nothing else, remember: Jenkins can automatically delete old builds and artifacts to save disk space using build discarders.

Common Mistakes
Not setting build discarders in the Jenkinsfile or job configuration
Old builds and artifacts accumulate, filling disk space and causing Jenkins to slow down or fail builds.
Always configure build discarders with limits on how many builds and artifacts to keep.
Setting very high numbers for builds to keep, like 1000
This defeats the purpose of cleanup and still uses too much disk space.
Choose reasonable limits based on your storage capacity and project needs, like keeping only the last 10 builds.
Manually deleting build folders from Jenkins home directory
This can corrupt Jenkins metadata and cause errors.
Use Jenkins UI or configuration options to safely discard old builds.
Summary
Use the buildDiscarder option in Jenkinsfile to automatically clean old builds and artifacts.
Use Jenkins CLI commands to list jobs, get and update job configurations for disk space management.
Verify build cleanup by listing builds before and after applying discarders.

Practice

(1/5)
1. What is the main purpose of managing disk space in Jenkins?
easy
A. To reduce the number of Jenkins plugins installed
B. To increase the number of Jenkins users
C. To speed up the Jenkins UI loading time
D. To keep Jenkins builds running smoothly without storage issues

Solution

  1. Step 1: Understand Jenkins build process

    Jenkins stores build files and logs in workspaces which consume disk space.
  2. Step 2: Identify the effect of disk space on builds

    If disk space runs out, builds can fail or Jenkins may slow down.
  3. Final Answer:

    To keep Jenkins builds running smoothly without storage issues -> Option D
  4. Quick Check:

    Disk space management = smooth builds [OK]
Hint: Disk space issues cause build failures, manage to avoid them [OK]
Common Mistakes:
  • Thinking disk space affects only UI speed
  • Confusing disk space with user management
  • Assuming plugins control disk space
2. Which Jenkins pipeline step is used to clean the workspace and free disk space?
easy
A. deleteDir()
B. cleanWs()
C. removeWorkspace()
D. clearDisk()

Solution

  1. Step 1: Recall Jenkins pipeline steps for workspace cleanup

    The step cleanWs() is designed to delete all files in the current workspace.
  2. Step 2: Verify other options

    deleteDir() deletes the current directory but is less specific; others are not valid Jenkins steps.
  3. Final Answer:

    cleanWs() -> Option B
  4. Quick Check:

    Workspace cleanup step = cleanWs() [OK]
Hint: Remember cleanWs() cleans workspace files in pipelines [OK]
Common Mistakes:
  • Using deleteDir() which deletes current dir but not workspace safely
  • Assuming removeWorkspace() or clearDisk() exist
  • Confusing cleanWs() with plugin names
3. What will happen if you add cleanWs() at the end of a Jenkins pipeline script?
medium
A. The build will fail due to missing workspace files
B. The workspace files will be backed up automatically
C. The workspace files will be deleted after the build completes
D. Nothing happens; cleanWs() is ignored

Solution

  1. Step 1: Understand cleanWs() behavior

    cleanWs() deletes all files in the workspace where the build runs.
  2. Step 2: Consider placement at pipeline end

    Placing cleanWs() at the end deletes files after build finishes, freeing disk space.
  3. Final Answer:

    The workspace files will be deleted after the build completes -> Option C
  4. Quick Check:

    cleanWs() at end = workspace cleaned [OK]
Hint: cleanWs() deletes workspace files after build ends [OK]
Common Mistakes:
  • Thinking cleanWs() backs up files
  • Assuming it causes build failure
  • Believing cleanWs() does nothing
4. You added cleanWs() in your Jenkins pipeline, but disk space is still filling up. What is a likely cause?
medium
A. The Disk Usage Plugin is not installed or configured
B. cleanWs() only deletes files older than 7 days
C. cleanWs() does not delete files if the build fails
D. cleanWs() requires manual confirmation to delete files

Solution

  1. Step 1: Understand cleanWs() limitations

    cleanWs() deletes workspace files but does not manage other disk usage like old builds or logs.
  2. Step 2: Role of Disk Usage Plugin

    The Disk Usage Plugin helps monitor and automate cleanup beyond workspace, including old builds and artifacts.
  3. Final Answer:

    The Disk Usage Plugin is not installed or configured -> Option A
  4. Quick Check:

    Disk Usage Plugin needed for full cleanup [OK]
Hint: Use Disk Usage Plugin for full disk cleanup automation [OK]
Common Mistakes:
  • Believing cleanWs() deletes all disk files
  • Thinking cleanWs() deletes only old files
  • Assuming cleanWs() needs manual approval
5. How can you automate disk space cleanup in Jenkins to delete old builds and workspace files regularly?
hard
A. Configure the Disk Usage Plugin and add cleanWs() in pipelines
B. Manually delete workspace files after each build
C. Disable all Jenkins plugins to reduce disk usage
D. Increase disk size instead of cleaning files

Solution

  1. Step 1: Use Disk Usage Plugin for monitoring and cleanup

    This plugin tracks disk usage and can be set to delete old builds automatically.
  2. Step 2: Use cleanWs() in pipelines for workspace cleanup

    Adding cleanWs() deletes workspace files after builds, freeing space regularly.
  3. Final Answer:

    Configure the Disk Usage Plugin and add cleanWs() in pipelines -> Option A
  4. Quick Check:

    Plugin + cleanWs() = automated cleanup [OK]
Hint: Combine Disk Usage Plugin with cleanWs() for automation [OK]
Common Mistakes:
  • Relying on manual deletion only
  • Disabling plugins instead of cleaning
  • Ignoring disk size limits
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard