private static void cleanTree(ProgramModule module, Consumer<ProgramModule> renamer,
			Set<ProgramModule> visited) throws NotEmptyException {
		if (module == null || visited.contains(module)) {
			return;
		}

		visited.add(module);
		if (module.getNumChildren() == 0) {
			return;
		}

		// for each child, if fragment and empty delete the child otherwise, clean the child
		// if module has no children, delete it
		Group[] children = module.getChildren();
		for (Group child : children) {
			if (child instanceof ProgramModule) {
				ProgramModule childModule = (ProgramModule) child;
				cleanTree(childModule, renamer, visited);
				if (childModule.getNumChildren() == 0) {
					module.removeChild(childModule.getName());
				}
			}
			else {
				ProgramFragment fragment = (ProgramFragment) child;
				if (fragment.isEmpty()) {
					module.removeChild(fragment.getName());
				}
			}
		}

		renamer.accept(module);
	}
Method cleanTree. Removes all empty fragments and empty modules

@param module the root branch to be cleaned. Can be branch or entire tree
@throws NotEmptyException if attempting to remove a module or fragment that is not empty
Ghidra/Features/Base/src/main/java/ghidra/app/cmd/module/AbstractModularizationCmd.java