Clean up MultiBranch Pipelines that have been disabled

Created by Tass Skoudros, Modified on Thu, 11 Jul at 6:55 AM by Tass Skoudros

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.



Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article