Answers: <p>Found it by myself, if anyone will be interested check:</p>
  
  <p>getAssignedLabel()</p>
  
  <p>in. 
  <a href="https://javadoc.jenkins-ci.org/hudson/model/Queue.Item.html" rel="nofollow noreferrer">https://javadoc.jenkins-ci.org/hudson/model/Queue.Item.html</a></p>
  
Answers: CC BY-SA 4.0
Answers: 2019-11-13T13:03:48.657Z
Answers: 2019-11-13T13:03:48.657Z
Answers: mikazz
Body: <p>I'm freshman in Jenkins. I would like to cancel jobs <strong>by it's own label</strong> (hundreds) from my Queue of tasks given to slaves: <code>jenkins.model.Jenkins.getInstance().getQueue()</code> 
  so i then could do: <code>items[i].doCancelQueue()</code></p>
  
  <p>Currently I only know how to do it, by its <strong>name</strong>: <code>items[i].task.getFullName()</code>. </p>
  
  <p>I'm looking for something like this: (when I iterate over Slaves that's just give me list of available labels for a single slave)</p>
  
  <pre><code>for (aSlave in hudson.model.Hudson.instance.slaves) {
      println('Name: ' + aSlave.name);
      println('getLabelString: ' + aSlave.getLabelString());
  }
  </code></pre>
  
  <p>My whole code for deleting jobs by name</p>
  
  <pre><code>import hudson.model.*  
  
  def q = jenkins.model.Jenkins.getInstance().getQueue()   
  def items = q.getItems()
  
  if (job_name_fragment == '' &amp;&amp; revision == ''){
      println("At least one param is requied")
      return 1
  }
  
  def count = 0
  for (i=0;i&lt;items.length;i++){  
      if (items[i].task.getName().contains("trunk") || items[i].task.getName().contains("dev")|| items[i].task.getName().contains("Windows")){
          println("Task: " + items[i].task.getName() + " has been skipped")
          continue
      }
      if(job_name_fragment == '' || items[i].task.getFullName().contains(job_name_fragment)){  
          if(revision == '' || items[i].params.contains(revision)){
              println("Task: " + items[i].task.getFullName() + " has been deleted from queue")
              items[i].doCancelQueue()  
              count++;
          }
      }
  }
  println("")
  println("Removed total: " + count + " tasks from queue")
  </code></pre>
  
  <p>Thanks</p>
  
Tags: <jenkins><jenkins-pipeline>
Title: Jenkins Cancel (jobs) from Queue by Label
UserDisplayName: mikazz
ThreadText: # Jenkins Cancel (jobs) from Queue by Label
  
  (Asked by: username_0 on 2019-11-13)
  
  I'm freshman in Jenkins. I would like to cancel jobs **by it's own label** (hundreds) from my Queue of tasks given to slaves: `jenkins.model.Jenkins.getInstance().getQueue()`
  so i then could do: `items[i].doCancelQueue()`
  
  Currently I only know how to do it, by its **name**: `items[i].task.getFullName()`.
  
  I'm looking for something like this: (when I iterate over Slaves that's just give me list of available labels for a single slave)
  
  ```
  for (aSlave in hudson.model.Hudson.instance.slaves) {
      println('Name: ' + aSlave.name);
      println('getLabelString: ' + aSlave.getLabelString());
  }
  
  ```
  
  My whole code for deleting jobs by name
  
  ```
  import hudson.model.*  
  
  def q = jenkins.model.Jenkins.getInstance().getQueue()   
  def items = q.getItems()
  
  if (job_name_fragment == '' && revision == ''){
      println("At least one param is requied")
      return 1
  }
  
  def count = 0
  for (i=0;i<items.length;i++){  
      if (items[i].task.getName().contains("trunk") || items[i].task.getName().contains("dev")|| items[i].task.getName().contains("Windows")){
          println("Task: " + items[i].task.getName() + " has been skipped")
          continue
      }
      if(job_name_fragment == '' || items[i].task.getFullName().contains(job_name_fragment)){  
          if(revision == '' || items[i].params.contains(revision)){
              println("Task: " + items[i].task.getFullName() + " has been deleted from queue")
              items[i].doCancelQueue()  
              count++;
          }
      }
  }
  println("")
  println("Removed total: " + count + " tasks from queue")
  
  ```
  
  Thanks
  
  **Tags:** jenkins, jenkins-pipeline
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2019-11-13. Score: 2 | Accepted answer
  
  Found it by myself, if anyone will be interested check:
  
  getAssignedLabel()
  
  in.
  <https://javadoc.jenkins-ci.org/hudson/model/Queue.Item.html>
  
  
  ---
  
  