How can I purge/clean the build queue?

Article ID:360051376772
Last Reviewed:2020-10-26()
2 minute readKnowledge base

Issue

You want to purge the build queue removing all the queued builds, or specifically remove some of them by name.

Resolution

The builds in the pending queue can be easily cancelled from the Jenkins UI by manually clicking on the red "x" icon next to each build. But if you need to cancel all of them, or only the ones matching a specific criteria, manuall cancelling them may not be fesible, most of all if the queue contains many of those pending builds.

  1. In order to clean all pending builds from the queue, you can run the below script from the script console:

Jenkins.instance.queue.clear()
  1. If you need to remove only some of them , you can use the below script to purge the build queue by build name.

import hudson.model.*
def q = Jenkins.instance.queue
q.items.findAll { it.task.name.startsWith('REPLACEME') }.each { q.cancel(it.task) }

Please note that depending on the build size and the instance performance, this operation may take longer than expected, as you may likely be facing a performance issue, or in a situation with a very large build queue, that will take longer to be purged.

If you are having difficulty starting Jenkins due to a large build queue, you can refer to our How can I prevent jenkins from starting new jobs after a restart? article to prevent Jenkins from automatically restarting queued jobs.