Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Clearing all jobs workspaces to get free up disk space

Step-by-step guide


...

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


 script to delete old builds

Here is a sample Groovy script you can use

Code Block
import hudson.model.*

def jobName = "YOUR_JOB_NAME" // Replace with your job name
def daysToKeep = 20

def job = Jenkins.instance.getItem(jobName)

job.getItems().each { jobItem ->
    if (jobItem instanceof Job) {
        jobItem.getBuilds().findAll { build ->
            def buildDate = new Date(build.getTimeInMillis())
            def currentDate = new Date()
            def diffDays = (currentDate - buildDate) / (1000 * 60 * 60 * 24)
            diffDays > daysToKeep
        }.each { build ->
            println "Deleting build #${build.number} (${build.time})"
            build.delete()
        }
    }
}


Make sure to replace `"YOUR_JOB_NAME"` with the name of your multibranch pipeline job. and daysToKeep with something you want, here is 20 days

Jira
serverTDS Jira
columnIdsissuekey,summary,issuetype,created,updated,duedate,assignee,reporter,priority,status,resolution
columnskey,summary,type,created,updated,due,assignee,reporter,priority,status,resolution
serverIdcb20bf71-ddc4-3b26-829a-cf167a6fabdb
keySDTDS-43819
=> here it was requested

...