code: public static void deleteChildrenNotInCollection(Resource resource, ConfigurationCollectionPersistData data) {
      Set<String> collectionItemNames = data.getItems().stream()
          .map(item -> item.getCollectionItemName())
          .collect(Collectors.toSet());
  
      for (Resource child : resource.getChildren()) {
        if (!collectionItemNames.contains(child.getName()) && !StringUtils.equals(JCR_CONTENT, child.getName())) {
          deletePageOrResource(child);
        }
      }
    }
docs: Delete children that are no longer contained in list of collection items.
  @param resource Parent resource
  @param data List of collection items