-
-
Save ceilfors/1400fd590632db1f51ca to your computer and use it in GitHub Desktop.
import com.cloudbees.hudson.plugins.folder.Folder | |
import hudson.FilePath | |
import jenkins.model.Jenkins | |
def boolean isFolder(String name) { | |
def item = Jenkins.instance.getItemByFullName(name) | |
return item instanceof Folder | |
} | |
def deleteUnusedWorkspace(FilePath root, String path) { | |
root.list().each { child -> | |
String fullName = path + child.name | |
if (isFolder(fullName)) { | |
deleteUnusedWorkspace(root.child(child.name), "$fullName/") | |
} else { | |
if (Jenkins.instance.getItemByFullName(fullName) == null) { | |
println "Deleting: $fullName " | |
child.deleteRecursive() | |
} | |
} | |
} | |
} | |
for (node in Jenkins.instance.nodes) { | |
println "Processing $node.displayName" | |
def workspaceRoot = node.rootPath.child("workspace"); | |
deleteUnusedWorkspace(workspaceRoot, "") | |
} |
Thanks a lot for this! 👍
Hey from where do we run this script, and how to run. Im new to devops, plz help.
Run this script in "Script Console".
@devops04cloud and @tastewind,
Open Jenkins >> Manage Jenkins
then scroll down in the options list and look for "Script Console"
Hope this will help you!
Thank you
Why isFolder is being considered instead of deleting directly if the path doesn't exist in jenkins master
Hi @ceilfors ,
Thank you for help and sharing your script to solve everyone's problem.
I have a question though
Will this groovy script check for the (already) deleted jobs in my master and delete only the corresponding workspace both in master and its slaves?
or
Will this groovy script delete whatever jobs in my master and delete all the corresponding workspace both in master and its slaves?
Looking forward to your reply :-)
Hi @ceilfors
Thanks for sharing this.
If I am correct then it helps deleting the unused workspace from all the slave nodes, how about the master itself ?
Sorry I missed all previous comments. I haven't used Jenkins for a while. You can comment on the code that deletes the workspace and see what "println "Processing $node.displayName"" would print out before you proceed.
Hi @ceilfors
I did the same and I see it prints the slave node name. Anyways, I would read some Jenkins scripting docs to find out about the master.
Thank you again!
Hey from where do we run this script, and how to run. Im new to devops, plz help.
Or you can just type : https://jenkins.url.lan/script ( add script at the end of url )
You'll arrive in console script
Hey from where do we run this script, and how to run. Im new to devops, plz help.