Restore multiple items from sitecore recycle bin using the API

Have you ever messed up and couldn’t be bothered cleaning up in the ‘accepted’ way? I accidentally removed a large number of items from the sitecore media library. Restore the database? Maybe there is a faster way. Fortunately sitecore comes with a recycle bin. Unfortunately the recycle bin is not meant for cleaning up dumb mistakes.

When you delete one item that has subitems, all is great. The deleted item shows up in the recycle bin. When you restore the item, all subitems are restored as well. When the items are deleted one by one, they have to be restored one by one. Fortunately in the sitecore recycle bin you can select multiple items on a page to restore. Unfortunately this is limited to 15 items per page.

sitecore recycle bin

I had to restore 300+ items. To restore these by hand would mean scrolling through numerous pages to restore the items, 15 items at the time. Fortunately the sitecore API provides simple access to the recycle bin. This code on stackoverflow saved me from going through all the items by hand. I modified it so I could restore all items deleted after a certain date where the path started with a certain prefix.

The code to restore items from recycle bin using the sitecore API

using (new SecurityDisabler())
    DateTime archiveDate = new DateTime(2015, 9, 8);
    string pathPrefix = "/sitecore/media library";

    // get the recyclebin for the master database
    Sitecore.Data.Archiving.Archive archive = Sitecore.Data.Database.GetDatabase("master").Archives["recyclebin"];

    // get as many deleted items as possible 
    // where the archived date is after a given date 
    // and the item path starts with a given path
    var itemsRemovedAfterSomeDate =
        archive.GetEntries(0, int.MaxValue)
                .Where(entry => 
                    entry.ArchiveDate > archiveDate && 

    foreach (var itemRemoved in itemsRemovedAfterSomeDate)
        // restore the item

One comment

  1. Nice. Might come in handy sometimes 🙂

    Did you know that you can also change the pagesize for the ComponentArt grids in sitecore?
    You have to change the default pagesize in the web.config (search for GridPageSize).
    Default is 15, but you can change it to 50

    Next, you need to modify the xaml.xml file.
    Eg: \sitecore\shell\Applications\Archives\RecycleBin\RecycleBin.xaml.xml
    Search for pagesize and change it to 50.

    Cheers, Nico

Leave a Reply

Your email address will not be published. Required fields are marked *