Skip to content

Instantly share code, notes, and snippets.

@rb2k
Last active Nov 17, 2022
Embed
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)
}
}
@pbuckley4192
Copy link

pbuckley4192 commented Jul 2, 2015

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

@wilkko
Copy link

wilkko commented Jul 29, 2015

Great script! Line 14 is unused.

@SankarB
Copy link

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
Copy link

akomakom commented Jan 4, 2016

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

@frederic-meyrou
Copy link

frederic-meyrou commented Jan 26, 2016

Hi,

Here is the code to exclude Folders :

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

Enjoy.

./Fred

@faizan002
Copy link

faizan002 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
Copy link

kmayfiel-ibm commented May 10, 2016

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

@android-dev-xp
Copy link

android-dev-xp commented Aug 1, 2016

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/

@jskov-jyskebank-dk
Copy link

jskov-jyskebank-dk commented Aug 2, 2016

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
Copy link

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.

@rompic
Copy link

rompic commented Nov 23, 2016

@prasathsivasubramaniyan
Copy link

prasathsivasubramaniyan commented Feb 17, 2017

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

@dbmcgrew
Copy link

dbmcgrew commented Aug 8, 2017

Thanks for the script. Thanks too to jskovjyskebankdk for suggesting Jenkins.instance.getAllItems(). That was one of the enhancements I added to get the script to work in our environment.

@arunvsankar
Copy link

arunvsankar commented Sep 17, 2017

I am just getting started on Admin on Jenkins. I wanted to know what is lost when we do such a cleanup, what is not shown after the cleanup in the Jenkins UI.

@vancona
Copy link

vancona commented Oct 6, 2017

Hi guys,
with the latest upgrade of the Groovy plugin "setTemporarilyOffline" takes a RejectedAccessException due the new security fencing.
Does anyone have found a solution for that? Thanks.

@dapperdanman
Copy link

dapperdanman commented Feb 14, 2018

I get an error on Pipeline jobs that are present:

groovy.lang.MissingMethodException: No signature of method: org.jenkinsci.plugins.workflow.job.WorkflowJob.getCustomWorkspace() is applicable for argument types: () values: []
	at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:58)
	at org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:49)
	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
	at hudson.model.AbstractProject$getCustomWorkspace.call(Unknown Source)
	at Script1.run(Script1.groovy:54)
	at groovy.lang.GroovyShell.evaluate(GroovyShell.java:585)
	at groovy.lang.GroovyShell.evaluate(GroovyShell.java:623)
	at groovy.lang.GroovyShell.evaluate(GroovyShell.java:594)
	at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript.evaluate(SecureGroovyScript.java:170)
	at hudson.plugins.groovy.SystemGroovy.run(SystemGroovy.java:95)
	at hudson.plugins.groovy.SystemGroovy.perform(SystemGroovy.java:59)
	at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
	at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:779)
	at hudson.model.Build$BuildExecution.build(Build.java:205)
	at hudson.model.Build$BuildExecution.doRun(Build.java:162)
	at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:534)
	at hudson.model.Run.execute(Run.java:1720)
	at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
	at hudson.model.ResourceController.execute(ResourceController.java:98)
	at hudson.model.Executor.run(Executor.java:404)

@zerogvt
Copy link

zerogvt commented Feb 22, 2018

@dapperdanman see my fork for a solution for Workflow jobs (based on this SO thread).
Also incorporating @akomakom's solution for missing isBuilding() method.

@rb2k great script 👍

@TheNitek
Copy link

TheNitek commented Mar 5, 2018

@rb2k
Copy link
Author

rb2k commented Apr 8, 2019

Oh wow, I don't recall ever receiving email notifications for all these comments.

Someone just emailed me asking for a license clarification (!). So here we go:

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.

In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

I assume they'd probably want to use one of the forks though :)

@rpmet523
Copy link

rpmet523 commented Aug 21, 2020

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.

Can you please send me your code for this

@casmer
Copy link

casmer commented Mar 15, 2021

I made this version if you have 'folders' plugins installed, but does not have the size check, I did this for a server migration that included updates to svn and wanted to delete all workspaces. it handles the recursive nature of folder orginization.

// 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.
;

def deleteWorkspace (items, node) {
for (item in items) {
if (item.class.canonicalName != null
&& item.class.canonicalName != "com.cloudbees.hudson.plugins.folder.Folder"
&& item.class.canonicalName != "org.jenkinsci.plugins.workflow.job.WorkflowJob"
&& item.class.canonicalName != "com.github.mjdetullio.jenkins.plugins.multibranch.MavenMultiBranchProject"
&& item.class.canonicalName != "org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject"
&& item.class.canonicalName != "hudson.model.ExternalJob") {
println("Item of type "+item.class.canonicalName+" found")
if(!item.isBuilding()) {
println("Wiping out workspace of job "+item.name)
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)
}
} else {
println("Skipping job "+item.name+", currently building")
}

} else if (item.class.canonicalName == "com.cloudbees.hudson.plugins.folder.Folder") {
println("Item is folder with name "+item.name)
deleteWorkspace(((com.cloudbees.hudson.plugins.folder.Folder) item).getItems(), node)
} else {
println("Item of type "+item.class.canonicalName + " cannot have its workspace cleaned")
}
}
}

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")

    computer.setTemporarilyOffline(true, new hudson.slaves.OfflineCause.ByCLI("disk cleanup"))
    deleteWorkspace(Jenkins.instance.items, node)
       
    
    computer.setTemporarilyOffline(false, null)

}

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