SOLVED

Empty Corporate OneDrive Recycle Bins from PowerShell

Copper Contributor

I am looking for a simple method (possibly a PowerShell script) that will completely empty OneDrive recycle bins (incl. the secondary recycle bins) within a given URL `[tenant]-my.sharepoint.com/personal/[username]`; one user at-a-time is an okay solution, as we only need to do this in certain circumstances. I tried using SPO Management Shell, but the app told me that the OneDrive locations are not managed paths within SPO. Using the online interface will only handle 200 files at a time, with a 30 second processing time in between; sometimes we have upwards of 10,000 files in these recycle bins to remove, so this is not an appropriate method. Any ideas are appreciated. Thank you, in advance!

2 Replies
best response confirmed by Charisma Riley (Copper Contributor)
Solution
Use CSOM API in your code or PnP cmdlets instead

@jcgonzalezmartin Thank you. What would a script look like that empties both the regular and secondary recycle bin for the OneDrive account in question? Would the URL remain `[tenant]-my.sharepoint.com/personal/[username]`? The script I use for our SPO tenant looks like this (this one empties the regular recycle bin):

 

Try {    
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Ctx.Credentials = $Credentials
     
    #Move All Deleted Items to 2nd Stage Recycle bin
    $Ctx.Site.RecycleBin.MoveAllToSecondStage()
    $Ctx.ExecuteQuery()
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

How can I modify this for a user's OneDrive? Thanks again!

1 best response

Accepted Solutions
best response confirmed by Charisma Riley (Copper Contributor)
Solution
Use CSOM API in your code or PnP cmdlets instead

View solution in original post