Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 4 down vote accepted
+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());
}
share|improve this answer
    
How do I go about finding the ID if I only have the old filename? –  ElvisLikeBear Aug 30 '12 at 7:47
    
You have to loop through all the Items and check DirName and LeafName –  Per Jakobsen Aug 30 '12 at 7:54
    
Could you provide an example? I'm having no success doing this –  ElvisLikeBear Sep 2 '12 at 23:19
    
Can't award the bounty for another six hours but this looks to work - thank-you! –  ElvisLikeBear Sep 3 '12 at 7:12
    
Example added to answer –  Per Jakobsen Sep 3 '12 at 7:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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