We recently had a client ask us to restore a SharePoint Online site they had deleted. The site had been deleted about 30 days before, and we knew the day of the deletion. When we went to the recycle bin and looked for the site, we didn’t see it; even when looking in the second-stage recycle bin.
Because of the volume of items the client had deleted, we created an application to iterate through the recycle bin and locate the site based on the name of the site and the user who deleted it.
The code below also handles MFA authentication for the account used to run the application. Hopefully you will find this helpful if you ever run into the same issue.
static void GetItemsSPOSiteRecycleBin(string sSiteUrl)
//Try Catch block for error handling
Console.WriteLine(“Getting all items in the Recycle Bin of a SPO Site”);
//#Special Class to authenticate to SharePoint online with MFA. Will pop open a window similar to browser for entering username and password and allow for interacting with MFA type logins.
var authenticationManager =
//# Connect to site
ClientContext context =
//#Get web object
Web web = context.Web;
User user = web.CurrentUser;
var spoWeb = web;
//#Get Contents of Site Collection recycle bin
var spoRecycleBinItemCollection = context.Site.RecycleBin;
//#Loop through recycle bin items
foreach (var spoRecycleBinItem in spoRecycleBinItemCollection)
//# If url contains SITE NAME and was deleted by USER, in the code example, the name of the site is “wiki” and the user is “John”
if (spoRecycleBinItem.LeafName.ToLower().IndexOf(“wiki”) >= 0 && spoRecycleBinItem.DeletedByName.ToLower().IndexOf(“John“) >= 0)
//Restore the item to original location
//#Write out to console the name of the item
Console.WriteLine(“Item:” + spoRecycleBinItem.Title + “- Deleted By:” + spoRecycleBinItem.DeletedByName + “- Deleted Date:” + spoRecycleBinItem.DeletedDate);
//#Destroy the item to clear up memory
catch (Exception ex)
//Starting procedure for app
static void Main(string args)
//#Site Collection url, insert your Site Collection URL
var sSiteUrl = “https://demo.sharepoint.com“;
//Call GetItemsSPOSiteRecylceBin procedure passing the parameters for sSiteURL
SharePoint is an incredibly flexible platform that can solve many different challenges facing your organization—and improve the efficiency of your IT department. Our SharePoint architects will work with you to design a solution to meet your specific goals, implement that solution for you, and work with your team on user training and adoption planning. Reach out to us at firstname.lastname@example.org to connect and learn more – we are happy to help!