How can I cleanup the workspace in Jenkins? I am using AccuRev
as version control tool.
I created freestyle
projects in Jenkins.
How can I cleanup the workspace in Jenkins? I am using AccuRev
as version control tool.
I created freestyle
projects in Jenkins.
There is a way to cleanup workspace in Jenkins. You can clean up the workspace before build or after build.
First, install Workspace Cleanup Plugin.
To clean up the workspace before build: Under Build Environment, check the box that says Delete workspace before build starts.
To clean up the workspace after the build: Under the heading Post-build Actions select Delete workspace when build is done from the Add Post-build Actions drop down menu.
If you want to manually clean it up, for me with my version of jenkins (didn't appear to need an extra plugin installed, but who knows), there is a "workspace" link on the left column, click on your project, then on "workspace", then a "Wipe out current workspace" link appears beneath it on the left hand side column.
In Jenkins file add
cleanWs()
This will delete the workspace after the build is complete
Beside above solutions, there is a more "COMMON" way - directly delete the largest space consumer from Linux machine. You can follow the below steps:
Using ls -lart to list out hidden folder also, normally jenkin installation is placed in .jenkins/ folder
[xxxxx ~]$ ls -lart drwxrwxr-x 12 xxxx 4096 Feb 8 02:08 .jenkins/
Use df -h
to show Disk space in high level
du -sh ./*/
to list out total memory for each subfolder in current path.
du -a /etc/ | sort -n -r | head -n 10
will list top 10 directories eating disk space in /etc/
Normally ./job/ folder or ./workspace/ folder can be the largest folder. Please go inside and delete base on you need (DO NOT delete entire folder).
rm -rf theFolderToDelete
IMPORTANT: It is safe to remove the workspace for a given Jenkins job as long as the job is not currently running!
NOTE: I am assuming your $JENKINS_HOME
is set to the default: /var/jenkins_home
.
rm -rf /var/jenkins_home/workspaces/<workspace>
rm -rf /var/jenkins_home/workspaces/*
This one uses grep to create a whitelist:
ls /var/jenkins_home/workspace \
| grep -v -E '(job-to-skip|another-job-to-skip)$' \
| xargs -I {} rm -rf /var/jenkins_home/workspace/{}
This one uses du and sort to list workspaces in order of largest to smallest. Then, it uses head to grab the first 10:
du -d 1 /var/jenkins_home/workspace \
| sort -n -r \
| head -n 10 \
| xargs -I {} rm -rf /var/jenkins_home/workspace/{}
You can run the below script in the Manage Jenkins → Scripts Console for deleting the workspaces of all the jobs at one shot. We did this to clean up space on the file system.
import hudson.model.*
// For each project
for(item in Hudson.instance.items) {
// check that job is not building
if(!item.isBuilding()) {
println("Wiping out workspace of job "+item.name)
item.doDoWipeOutWorkspace()
}
else {
println("Skipping job "+item.name+", currently building")
}
}
There is an option to do it. Configure -> Post-build Actions
The option comes by default at least in Jenkins version 2.236
If you want to just remove one dir (or file) you can use Groovy and Manage Jenkins → Scripts Console run a script which removes it.
For example, you can look at the files with:
dh = new File('./bitnami/jenkins/jenkins_home/workspace/jobname/folder')
dh.eachFile {
println(it)
}
And then, when you have the folder you want to delete in dh
, you could simply append the next code:
dh.deleteDir()
And it will be deleted.
Note: The path shown in the example is for a Bitnami Jenkins installation, yours could be different.
not allowed to comment, therefore:
The answer from Upen works just fine, but not if you have Jenkins Pipeline Jobs mixed with Freestyle Jobs. There is no such Method as DoWipeWorkspace on Pipeline Jobs. So I modified the Script in order to skip those:
import hudson.model.*
import org.jenkinsci.plugins.workflow.job.WorkflowJob
// For each project
for(item in Hudson.instance.items) {
// check that job is not building
if(!item.isBuilding() && !(item instanceof WorkflowJob))
{
println("Wiping out workspace of job "+item.name)
item.doDoWipeOutWorkspace()
}
else {
println("Skipping job "+item.name+", currently building")
}
}
you could also filter by Job Names if required:
item.getDisplayName().toLowerCase().contains("release")
Assuming the question is about cleaning the workspace of the current job, you can run:
test -n "$WORKSPACE" && rm -rf "$WORKSPACE"/*
You will need to install this plugin before the options mentioned above will appear
This plugin add the check box to all job configs to allow you to delete the whole workspace before any steps (inc source control) are run
This is useful to make sure you always start from a known point to guarantee how you build will run
For those, who have builds within Docker under root
user, following method may help:
def cleanupWorkspaceAsRoot() {
log.notice "Removing workspace contents: $WORKSPACE !"
docker.image('alpine').inside('-u root --env WORKSPACE') { // use docker instead of sudo // TODO make ALL docker builds work as non-ROOT, so there is no root-owned files
sh 'test -n "$WORKSPACE" && rm -rf "$WORKSPACE"/*'
}
}
if you are using Windows as your agent, you can use.
rmdir /s /q "folder path ending with \"
and if you are using linux machine as agent, you can use,
rm -rvf "directory"
But I wouldn't suggest this approach as it will create multiple chances of conflict and build failures.
Best approach is to use pipeline dependency. Create a main pipeline to delete the directory of the pipeline you want to delete and run the pipeline once delete is successful. You can use triggers to start the new build once delete is completed or vice versa to delete the directory once the build is completed.
pipeline {
agent
{
node {
label 'master'
customWorkspace "${env.JobPath}"
}
}
stages
{
stage('Start') {
steps {
sh 'ls'
}
}
stage ('Invoke_pipeline') {
steps {
build job: 'pipeline1', parameters: [
string(name: 'param1', value: "value1")
]
}
}
stage('End') {
steps {
sh 'ls'
}
}
}
}
https://www.jenkins.io/doc/pipeline/steps/pipeline-build-step/
If you are using docker, it might create files (from within docker container) that are not owned by user jenkins, and thus you need to use sudo to remove them:
sh "sudo rm -rf \"$WORKSPACE\"/*"
sh "sudo rm -rf \"$WORKSPACE\"/.*"
For this to work, you need to add /etc/sudoers -permission for user jenkins (please edit /etc/sudoers) e.g (these are full permissions without password):
jenkins ALL=(ALL) NOPASSWD: ALL