Create a gist now

Instantly share code, notes, and snippets.

@rb2k /gist:8372402
Last active May 25, 2017

What would you like to do?
A jenkins script to clean up workspaces on slaves
// Check if a slave has < 10 GB of free space, wipe out workspaces if it does
import hudson.model.*;
import hudson.util.*;
import jenkins.model.*;
import hudson.FilePath.FileCallable;
import hudson.slaves.OfflineCause;
import hudson.node_monitors.*;
for (node in Jenkins.instance.nodes) {
computer = node.toComputer()
if (computer.getChannel() == null) continue
rootPath = node.getRootPath()
size = DiskSpaceMonitor.DESCRIPTOR.get(computer).size
roundedSize = size / (1024 * 1024 * 1024) as int
println("node: " + node.getDisplayName() + ", free space: " + roundedSize + "GB")
if (roundedSize < 10) {
computer.setTemporarilyOffline(true, new hudson.slaves.OfflineCause.ByCLI("disk cleanup"))
for (item in Jenkins.instance.items) {
jobName = item.getFullDisplayName()
if (item.isBuilding()) {
println(".. job " + jobName + " is currently running, skipped")
continue
}
println(".. wiping out workspaces of job " + jobName)
workspacePath = node.getWorkspaceFor(item)
if (workspacePath == null) {
println(".... could not get workspace path")
continue
}
println(".... workspace = " + workspacePath)
customWorkspace = item.getCustomWorkspace()
if (customWorkspace != null) {
workspacePath = node.getRootPath().child(customWorkspace)
println(".... custom workspace = " + workspacePath)
}
pathAsString = workspacePath.getRemote()
if (workspacePath.exists()) {
workspacePath.deleteRecursive()
println(".... deleted from location " + pathAsString)
} else {
println(".... nothing to delete at " + pathAsString)
}
}
computer.setTemporarilyOffline(false, null)
}
}

As someone who manages a 40 VM cluster all running jenkins slaves, I thank you!

wilkko commented Jul 29, 2015

Great script! Line 14 is unused.

SankarB commented Jan 1, 2016

Hi Marc Seeger,

This script is great but not working on cloudbee jenkins enterprise edition which is our environment. Facing issue with item.isBuilding(). I didn't know groovy scripting but i am in need of this script.

Error:

groovy.lang.MissingMethodException: No signature of method: com.cloudbees.hudson.plugins.folder.Folder.isBuilding() is applicable for argument types: () values: []

akomakom commented Jan 4, 2016

@SankarB, I made a fix for your issue (I also had this problem), see my fork.

Hi,

Here is the code to exclude Folders :

if (item instanceof com.cloudbees.hudson.plugins.folder.Folder)
{
// Folder
continue
}

Enjoy.

./Fred

faizan80 commented Apr 8, 2016

Hi,

Can anyone tell me, where can i execute this script ?

I would like to run this script as part of a cron job or something.
Any suggestions.

Br

kmayfiel-ibm commented May 10, 2016 edited

I believe you need the groovy plugin, and you need to run this on Master.
I haven't tried this script yet.

Hi Faizan80
Here is my post how to make Jenkins Groovy job and use the script above:
https://julienprog.wordpress.com/2016/04/14/clean-up-jenkins-workspaces/

You should change the loop to getAllItems() so it can be used on instances with Folders:

for (item in Jenkins.instance.getAllItems(Job.class)) {

I would also put the code following "setTemporarilyOffline" in a try-statement, with the offline disabling in a finally statement.

indrgun commented Oct 4, 2016

How do you delete old workspace for job that is either already deleted or renamed on Jenkins. They are still on the node and they are not checked by the script.

prasathsivasubramaniyan commented Feb 17, 2017 edited

How to cleanup tmp space of all slaves in the cloudbees jenkins using the groovy script less than 1days older files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment