Skip to the content

SPO - Restoring over 100k items from Recycle bin

Recently I was presented with the challenge of restoring over 100.000 items from SharePoint Online Recycle Bin.

A colleague accidentally deleted a synced folder in OneDrive for Business, without stopping the Sync.

Unfortunately if the Sync is still on the files are deleted also from the library in SPO.

Restoring from the Recycle Bin using SPO's UI is not an option when the number of items to restore is so large.

I tried to restore the deleted items using the PNP PowerShell command below:

Connect-PnPOnline-Urlhttps://TENANCY/sites/SITECOLLECTION_NAME-useWebLogin
$restoreSet=Get-PnPRecycleBinItem|Where-Object{ $_.DeletedByEmail -eq"EMAIL OF USER WHO DELETED THE ITEMS"-and$_.DeletedDate -gt"DATE FROM WHEN THE FILES DELETED (FORMAT MM/DD/YYYY"}
$restoreSet|Restore-PnPRecycleBinItem-Force

Unfortunately I was getting an error: “Restore-PnPRecycleBinItem -The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator“

It seems that the view threshold it's set to 100.000 items.

This is how I have managed to restore the deleted items from the recycle bin.

  1. Discover how many items a user has deleted for a specific site collection in a specified timeframe.
    Please replace SITECOLLECTION URL, EMAIL OF USER WHO DELETED THE ITEMS, DATE FROM WHEN THE FILES DELETED
     

    Connect-PnPOnline -Url SITECOLLECTION URL -useWebLogin 
    $restoreSet = Get-PnPRecycleBinItem | Where-Object { $_.DeletedByEmail -eq "EMAIL OF USER WHO DELETED THE ITEMS" -and $_.DeletedDate -gt "DATE FROM WHEN THE FILES DELETED (FORMAT MM/DD/YYYY" } 
    $restoreSet.Count 
  2. Export the details of the deleted items to a CSV file

    Connect-PnPOnline -Url SITECOLLECTION URL -useWebLogin
    $restoreSet = Get-PnPRecycleBinItem | Where-Object { $_.DeletedByEmail -eq "EMAIL OF USER WHO DELETED THE ITEMS" -and $_.DeletedDate -gt "DATE FROM WHEN THE FILES DELETED (FORMAT MM/DD/YYYY" }| Export-csv "C:\Temp\EXPORTFILENAME.csv"
  3. Open the CSV file with Excel and apply a filter on the column ItemType to exclude "Folder”. Any folder containing data will be re-created automatically anyway when a file is restored. 

  4. With the filter in place copy the columns Id and Title to a new sheet and save it as CSV. I called the file "FilesToRestore.csv".
  5. Run the following PowerShell script (courtesy of https://cann0nf0dder.wordpress.com/) to loop through every item in the file "FilesToRestore.csv" (step 4), and call “/_api/sites/RecycleBin/RestoreByIds” using Invoke-PnPSPRestMethod.

    When prompted enter the "Sitecollection" parameter !!remember the “/” at the end of URL!!  and the full path of the CSV file "FilesToRestore.csv" (step 4).
    [CmdletBinding(SupportsShouldProcess)] 
    
    param( 
    
        # The URL of the Sitecollection where the recycle bin is. 
    
        [Parameter(Mandatory)] 
    
        [string] 
    
        $SiteUrl, 
    
    
        # Full Path of CSV file 
    
        [Parameter(Mandatory)] 
    
        [string] 
    
        $Path 
    
    ) 
    
    function Restore-RecycleBinItem { 
    
        param( 
    
            [Parameter(Mandatory)] 
    
            [String] 
    
            $Id 
    
        ) 
    
    
        $siteUrl = (Get-PnPSite).Url 
    
        $apiCall = $siteUrl + "/_api/site/RecycleBin/RestoreByIds" 
    
        $body = "{""ids"":[""$Id""]}" 
      
    
        Write-Verbose "Performing API Call to Restore item from RecycleBin..." 
    
        try { 
    
            Invoke-PnPSPRestMethod -Method Post -Url $apiCall -Content $body | Out-Null 
    
        } 
    
        catch { 
    
            Write-Error "Unable to Restore ID {$Id}"      
    
        } 
    
    } 
    
    $ErrorActionPreference = 'Continue' 
    
    $InformationPreference = 'Continue' 
    
    Connect-PnPOnline -Url:$SiteUrl -UseWebLogin 
    
      
    @($(Import-Csv -Path:"$Path")).ForEach({ 
    
        $csv = $PSItem 
    
        Write-Information -MessageData:"Restore item $($csv.Title)" 
    
        Restore-RecycleBinItem -Id $($csv.ID) 
    
    }) 

You can check the progress of the restoring process by running the PowerShell command in step 1.
You should see the number decreasing while the files are being restored.

About the author

Giorgio Capisani

Giorgio Capisani

I am an experienced IT professional specialising in Microsoft technologies (SharePoint 2010/2013/Online, Power Platform, Office 365), with over 10 years of experience in large public and private sector organisations. 

comments powered by Disqus

Tech

Visit my blog

So you landed on my website … Why not have a look at my blog and leave a comment or ask a question.