> ## Knowledge Base Index
> Fetch the complete knowledge base index at: https://support.stacktrack.com/sitemap.xml
> Use this file to discover available pages before exploring further.
> Pure-Markdown content can be obtained by appending a '.md' suffix to the content URLs listed in the sitemap (without the trailing slash).

# 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.