Clean up MultiBranch Pipelines that have been disabled
For multi-branch pipelines, when the feature branch (or) pull request is closed in git. These are marked as disabled by multi-branch pipeline automatically, but Jenkins wont delete the job history. This script will check and delete the jobs and builds of disabled jobs.
import org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject
import hudson.model.Job
boolean dryRun = true
Jenkins.instance.getAllItems(WorkflowMultiBranchProject.class).findAll { WorkflowMultiBranchProject project ->
println('MultiBranch Project Name: ' + project.getUrl())
project.items.findAll { Job j ->
if (!j.isBuildable() ){
println(' Job Name: ' + j.name)
if(!dryRun) {
j.delete()
}
}
}
}
To execute this script go to your Jenkins controllers.
- Create a pipeline.
- Add the above code to the pipeline.
- Execute the pipeline. As dryRun is enabled it is possible to identify all the jobs that will be cleaned when it is disabled.
- You need to be a admin to execute this.
Updated on: 31/10/2025
Thank you!