0
0
Jenkinsdevops~5 mins

Disk space management in Jenkins - Commands & Configuration

Choose your learning style9 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.