Condividi tramite


Delete lots of items in the site collection Recycle Bin

It happened that after some time, the site collection Recycle Bin got "filled" with lots of items (in this particular case, we were talking about more than 4 million items). Furthermore, choosing the "Empty Recycle Bin" did not yield the expected results.

The first attempt to clear this out using Object Model code was also unsuccessful. Simply calling Console.WriteLine(SPSite.RecycleBin.Count) threw an odd exception. So we had to use a different approach:

 static void Main(string[] args) {
  if (args.Length == 2) {
  using (SPSite ms = new SPSite(args[0]))
  using (SPWeb mw = ms.OpenWeb()) {
    SPRecycleBinQuery qu = new SPRecycleBinQuery();
    qu.RowLimit = int.Parse(args[1]);
    qu.OrderBy = SPRecycleBinOrderBy.Default;
    SPRecycleBinItemCollection re = ms.GetRecycleBinItems(qu);
    try {
      re.DeleteAll();
    }
    catch (Exception ex) {
      Console.WriteLine(ex.Message);
    } } }
  else {
    Console.WriteLine("Usage: emptyrecbin.exe site-collection-url items");
  } }