Articles on: Jenkins Service

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.


  1. Create a pipeline.
  2. Add the above code to the pipeline.
  3. Execute the pipeline. As dryRun is enabled it is possible to identify all the jobs that will be cleaned when it is disabled.
  4. You need to be a admin to execute this.

Updated on: 31/10/2025

Was this article helpful?

Share your feedback

Cancel

Thank you!