2

I know I can empty the recycle bin for a site collection with the following:

public static bool EmptyRecycleBin(string siteCollection)
        {
            try
            {
                using (var site = new SPSite(siteCollection))
                {
                    site.RecycleBin.DeleteAll();
                }
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: {0}", ex);

                return false;
            }
        }

However this could cause issues and I'd like to only remove specific items (that I've deleted programatically) from here. Is there a way to do this or is it impossible?

1 Answer 1

4
+50

You have to find the ID SPRecycleBinItem and use SPRecycleBinItemCollection.Delete or find the SPRecycleBinItem and use SPRecycleBinItem.Delete

This code deletes any instances of Test.png from the folder Test in the documentlibrary with rootfolder TestDocs from the recyclebin:

using (var site = new SPSite("http://sp2010"))
{
    var idsToDelete = new List<Guid>();
    foreach (SPRecycleBinItem item in site.RecycleBin)
    {
        if (item.DirName.Equals("TestDocs/Test", StringComparison.OrdinalIgnoreCase)
         && item.LeafName.Equals("Test.png", StringComparison.OrdinalIgnoreCase))
        {
            idsToDelete.Add(item.ID);
        }
    }
    site.RecycleBin.Delete(idsToDelete.ToArray());
}
5
  • How do I go about finding the ID if I only have the old filename?
    – Michael A
    Aug 30, 2012 at 7:47
  • You have to loop through all the Items and check DirName and LeafName Aug 30, 2012 at 7:54
  • Could you provide an example? I'm having no success doing this
    – Michael A
    Sep 2, 2012 at 23:19
  • Can't award the bounty for another six hours but this looks to work - thank-you!
    – Michael A
    Sep 3, 2012 at 7:12
  • Example added to answer Sep 3, 2012 at 7:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.