How can I get a list of empty Folders and Multibranch jobs

Article ID:360055445652
Last Reviewed:2021-01-21()
1 minute readKnowledge base

Issue

You want to locate Folders/Multibranch jobs that are empty and no longer contains any item inside.

Resolution

Go to Manage Jenkins > Script Console and copy/paste the following script. It will print the name of all folders and Multibranch jobs with no items inside.

import com.cloudbees.hudson.plugins.folder.Folder
import com.cloudbees.hudson.plugins.folder.AbstractFolder

Jenkins.instance.getAllItems(AbstractFolder.class).each { folder ->
  if (isEmpty(folder)) {
    println folder.fullName + " - " + folder.class
  }
};

return

def isEmpty(folder) {
  return folder?.items?.size() == 0
}
This article is part of our Knowledge Base and is provided for guidance-based purposes only. The solutions or workarounds described here are not officially supported by CloudBees and may not be applicable in all environments. Use at your own discretion, and test changes in a safe environment before applying them to production systems.