private void purge(final Folder folder, final SearchFilter filter) throws Exception {
    final int num = folder.getChildFolderCount();
    if (includeSubfolders && num > 0) {
      final FolderView folderView = new FolderView(num);
      final FindFoldersResults children = folder.findFolders(folderView);
      for (final Folder child : children) {
        purge(child, filter);
        final ItemView view = new ItemView(1);
        if (child.findItems(view).getTotalCount() == 0) {
          child.delete(DeleteMode.HardDelete);
        }
      }
    }

    while (deleteItems(folder, filter)) {}
  }
 private boolean deleteItems(final Folder folder, final SearchFilter filter) throws Exception {
   final ItemView view = new ItemView(500);
   view.setPropertySet(PropertySet.IdOnly);
   final FindItemsResults<Item> items = folder.findItems(filter, view);
   if (items.getTotalCount() > 0) {
     final Iterable<ItemId> itemIds = Iterables.transform(items, ItemToItemId.INSTANCE);
     folder
         .getService()
         .deleteItems(
             itemIds,
             deleteMode,
             SendCancellationsMode.SendToNone,
             AffectedTaskOccurrence.SpecifiedOccurrenceOnly);
   }
   return items.isMoreAvailable();
 }