How to delete items from the SharePoint Recycle Bin using PowerShell when there are large number of items in the Recycle Bin?

Nemeth, Dan 0 Reputation points
2025-04-21T15:02:23.1966667+00:00

We just implemented retention labels on one of our very large SharePoint Online libraries, which caused the deletion of 800,000 files. Of course someone wanted a specific file restored, but it's impossible to scroll through the Recycle Bin web page to find the file. I wrote a PowerShell script to retrieve the items in the Recycle Bin which works. I was able to identify the item in the Recycle Bin using PowerShell and get a handle on it. I can return the properties of that item, but I cannot act upon it. I cannot restore it nor send it to the secondary recycle bin. The error returned is "The attempted operation is prohibited because it exceeds the list view threshold". So what I'm looking for is a method for restoring a file from a Recycle Bin that has a large number of items in it.

I have opened Microsoft Support ticket and they said they are asking internally (it has been 2 weeks) and that I should ask the question of the GitHub Community and here. So here it is!

Thanks in advance!

Dan Nemeth

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2025-04-21T15:25:52.98+00:00

    It would be helpful if you said how you were going about removing the item. Were you using its identity in the Clear-PnPRecycleBinItem cmdlet? Or are you using something else?


  2. Rich Matheisen 47,901 Reputation points
    2025-04-22T15:31:40.0166667+00:00

    I'm going to qualify this answer with two facts:

    1. I know next to nothing a SharePoint
    2. The code below comes from Microsoft CoPilot -- and I have no was to verify that it will actually work.
    # Define the site URL and item ID
    $siteUrl = "https://yoursharepointsiteurl"
    $itemId = "ItemID"
    # Connect to SharePoint
    Connect-PnPOnline -Url $siteUrl -Interactive
    # Retrieve the item details
    $response = Invoke-RestMethod -Uri "$siteUrl/_api/web/recyclebin('$itemId')" -Method Get -Headers @{"Accept"="application/json"}
    # Check if the item exists
    if ($response) {
        # Restore the item
        Invoke-RestMethod -Uri "$siteUrl/_api/web/recyclebin('$itemId')/restore()" -Method Post -Headers @{"Accept"="application/json"}
        Write-Host "Item restored successfully."
    } else {
        Write-Host "Item not found in the Recycle Bin."
    }
    

    I haven't been able to find any other way around the error you're encluntering.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.