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
}