code: public static void pruneMenus(BaseComponent parent) {
          while (parent != null && parent instanceof BaseMenuComponent) {
              if (parent.getChildren().isEmpty()) {
                  BaseComponent newParent = parent.getParent();
                  parent.destroy();
                  parent = newParent;
              } else {
                  break;
              }
          }
      }
docs: Recursively remove empty menu container elements. This is done after removing menu items to
  keep the menu structure lean.
  
  @param parent The starting menu container.