Cancel queued builds and aborting executing builds using Groovy for Jenkins Cancel queued builds and aborting executing builds using Groovy for Jenkins jenkins jenkins

Cancel queued builds and aborting executing builds using Groovy for Jenkins


I haven't tested it myself, but looking at the API it should be possible in the following way:

import hudson.model.*import jenkins.model.Jenkinsdef q = Jenkins.instance.queueq.items.findAll { it.task.name.startsWith('my') }.each { q.cancel(it.task) }

Relevant API links:


I know it's kind of an old question, but Google points me to this one. The scripts shown here only remove the jobs from the queue, and don't stop running builds. The following script, just removes everything from the queue and kills all running builds:

  import java.util.ArrayList  import hudson.model.*;  import jenkins.model.Jenkins  // Remove everything which is currently queued  def q = Jenkins.instance.queue  for (queued in Jenkins.instance.queue.items) {    q.cancel(queued.task)  }  // stop all the currently running jobs  for (job in Jenkins.instance.items) {    stopJobs(job)  }  def stopJobs(job) {    if (job in com.cloudbees.hudson.plugins.folder.Folder) {      for (child in job.items) {        stopJobs(child)      }        } else if (job in org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject) {      for (child in job.items) {        stopJobs(child)      }    } else if (job in org.jenkinsci.plugins.workflow.job.WorkflowJob) {      if (job.isBuilding()) {        for (build in job.builds) {        build.doKill()        }      }    }  }


Referencie: https://xanderx.com/post/cancel-all-queued-jenkins-jobs/

Run this in Manage Jenkins > Script Console:

Jenkins.instance.queue.clear()