Various ways to recover deleted item from Sitecore Recycle Bin and Setting

Posted 09/16/2016 by Ravi Manokaran

When we delete an item in Sitecore it is not deleted permanently. Instead it is moved to the recycle bin just like on a Windows PC recycle bin. Items sit in the Recycle bin until someone permanently deletes everything in it, meaning you can recover them if they were accidentally deleted.

The recycle bin can be accessed from the Desktop or the Launchpad. From the Desktop, click start and then we’ll notice the Recycle Bin link under the All Applications fly out.

Simply click an item and then click the Restore button to restore that item. We can also use search to find your items if there are too many to sort through. By default, only administrators can see items they themselves didn’t delete.

The Delete button will permanently delete the selected item in the recycle bin. The Empty button will permanently delete ALL items in the recycle bin. The Show Versions check box will expand versions of the items in the recycle bin in case we want to restore just one particular version of a deleted item, rather than all versions.

It is common for recycle bins to grow forever and become a nuisance for administrators and infrastructure team who is concerned about restorability and recovery times. We may prefer to simply disable the recycle bin rather than run the risk of it growing too large (not a best practice). In this case, a simple web.config change can do this. Disable the recycle bin in web.config by setting the RecycleBinActive setting to false, or for a given database, remove /archives/archive/recyclebin element.

When delete one item that has subitems, all is great. The deleted item shows up in the recycle bin. When 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 can select multiple items on a page to restore. Unfortunately this is limited to 15 items per page.

We recently 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 from going through all the items by hand. We modified it so we 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(2016, 7, 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 &&

                    entry.OriginalLocation.StartsWith(pathPrefix)

                ).ToList(); 

    foreach (var itemRemoved in itemsRemovedAfterSomeDate)

    {

        // restore the item

        archive.RestoreItem(itemRemoved.ArchivalId);

    }

   }

Share:

Archive

Syndication