Clearing all jobs workspaces to get free up disk space
Step-by-step guide
- Go to https://jenkins.PROJECTX.tds.CUSTOMERX.COM/script
Enter following script that clears workspaces on master:
Code Block |
---|
import hudson.model.*
deleteWorkspace (Hudson.instance.items)
def deleteWorkspace (items) {
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)
item.doDoWipeOutWorkspace()
} 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())
} else {
println("Item of type "+item.class.canonicalName + " cannot have its workspace cleaned")
}
}
} |
Eventually you can clear also all nodes/agents also:
Code Block |
---|
def deleted = []
def oneDayAgo = new Date() - 1
jenkins.model.Jenkins.instance.nodes.each { hudson.model.Node node ->
node.workspaceRoot.listDirectories().each { hudson.FilePath path ->
def pathName = path.getRemote()
if (path.name.startsWith(".")) {
println "Skipping internal dir $node.displayName:$pathName"
} else {
def lastModified = new Date(path.lastModified())
if (lastModified < oneDayAgo) {
println "Deleting workspace at $node.displayName:$pathName (last modified $lastModified)"
path.deleteRecursive()
deleted << "$node.displayName:$pathName"
} else {
println "Skipping workspace at $node.displayName:$pathName (last modified $lastModified)"
}
}
}
}
"Deleted workspaces: \n\t" + deleted.sort().join("\n\t") |
Related articles
Content by Label |
---|
showLabels | false |
---|
max | 5 |
---|
spaces | TDSKB |
---|
showSpace | false |
---|
sort | modified |
---|
reverse | true |
---|
type | page |
---|
cql | label = "kb-how-to-article" and type = "page" and space = "TDSKB" |
---|
labels | kb-how-to-article |
---|
|
...
...