When dealing with Jenkins storage management, it's crucial to understand the two key directories:
workspace/
- Contains active working copies during build executionjobs/
- Stores historical build data, configurations, and artifacts
Using deleteDir()
in the post-build section is a good start, but has limitations:
post {
always {
deleteDir() // Only cleans current workspace, not historical data
}
}
While you can manually delete contents, consider these precautions:
- Stop Jenkins service before deletion
- Back up
$JENKINS_HOME
entirely - Use Jenkins' built-in tools first
1. Using Discard Old Build Plugin
Configure in job settings:
options {
buildDiscarder(logRotator(
daysToKeepStr: '7',
numToKeepStr: '10',
artifactDaysToKeepStr: '7',
artifactNumToKeepStr: '5'
))
}
2. Groovy Script Console Cleanup
For bulk operations, run this in Script Console:
Jenkins.instance.getAllItems(Job.class).each { job ->
if (job.isBuildable()) {
job.builds.each { build ->
if (build.number < job.nextBuildNumber - 20) {
build.delete()
}
}
}
}
3. Workspace Cleanup Plugin
Add this to your pipeline:
cleanWs(
cleanWhenAborted: true,
cleanWhenFailure: true,
cleanWhenNotBuilt: true,
cleanWhenSuccess: true,
deleteDirs: true
)
If you must delete manually:
- Stop Jenkins:
sudo systemctl stop jenkins
- Navigate to
$JENKINS_HOME
- For workspaces:
rm -rf workspace/*
- For jobs:
find jobs -name "builds" -exec rm -rf {}/[0-9]* \;
- Restart Jenkins:
sudo systemctl start jenkins
Install the Disk Usage plugin to track space and set up alerts. Configure in Manage Jenkins > System Configuration.
When managing a Jenkins instance, it's crucial to understand two key directories under $JENKINS_HOME
:
jobs/
└── [job_name]/
├── builds/
├── workspace/
└── config.xml
workspace/
└── [job_name]/
└── [working_files]
While manually deleting files might seem straightforward, consider these safer methods:
Method 1: Using Built-in Jenkins Cleanup
The safest approach is through Jenkins' web interface:
- Navigate to
Manage Jenkins
→Script Console
- Run this Groovy script:
import hudson.model.*
import jenkins.model.Jenkins
// Clean workspaces
Jenkins.instance.computers.each { computer ->
computer.workspaceCleanup()
}
// Clean builds older than 30 days
Jenkins.instance.getAllItems(Job.class).each { job ->
job.builds.findAll { it.number.toInteger() < job.nextBuildNumber - 30 }.each {
it.delete()
}
}
Method 2: Pipeline Post-Action Cleanup
For ongoing maintenance, modify your pipelines:
pipeline {
agent any
stages {
stage('Build') {
steps {
// Your build steps
}
}
}
post {
always {
cleanWs() // Prefer over deleteDir() for Jenkins workspaces
script {
// Keep last 10 builds
currentBuild.getRawBuild().getParent().getBuilds()
.drop(10)
.each { build ->
build.delete()
}
}
}
}
}
If you must delete files manually:
# Recommended procedure for manual cleanup
cd $JENKINS_HOME
# For workspaces (safe to delete)
find workspace/ -mindepth 1 -maxdepth 1 -type d -exec rm -rf {} \;
# For job artifacts (more dangerous)
find jobs/ -path "*/builds/*" -not -path "*/last*" -type d -mtime +30 -exec rm -rf {} \;
Critical notes:
- Never delete the
jobs/[job_name]/config.xml
files - Stop Jenkins before major manual deletions
- Create backups before proceeding
For enterprise environments, consider these plugins:
# Recommended plugins
- Workspace Cleanup Plugin
- JobConfigHistory Plugin
- ThinBackup Plugin
Configuration example for Workspace Cleanup Plugin:
// In Jenkinsfile
cleanWs(
cleanWhenAborted: true,
cleanWhenFailure: true,
cleanWhenNotBuilt: true,
cleanWhenSuccess: true,
deleteDirs: true
)