SOLVED

Empty Corporate OneDrive Recycle Bins from PowerShell

%3CLINGO-SUB%20id%3D%22lingo-sub-1092957%22%20slang%3D%22en-US%22%3EEmpty%20Corporate%20OneDrive%20Recycle%20Bins%20from%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1092957%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20looking%20for%20a%20simple%20method%20(possibly%20a%20PowerShell%20script)%20that%20will%20completely%20empty%20OneDrive%20recycle%20bins%20(incl.%20the%20secondary%20recycle%20bins)%20within%20a%20given%20URL%20%60%5Btenant%5D-my.sharepoint.com%2Fpersonal%2F%5Busername%5D%60%3B%20one%20user%20at-a-time%20is%20an%20okay%20solution%2C%20as%20we%20only%20need%20to%20do%20this%20in%20certain%20circumstances.%20I%20tried%20using%20SPO%20Management%20Shell%2C%20but%20the%20app%20told%20me%20that%20the%20OneDrive%20locations%20are%20not%20managed%20paths%20within%20SPO.%20Using%20the%20online%20interface%20will%20only%20handle%20200%20files%20at%20a%20time%2C%20with%20a%2030%20second%20processing%20time%20in%20between%3B%20sometimes%20we%20have%20upwards%20of%2010%2C000%20files%20in%20these%20recycle%20bins%20to%20remove%2C%20so%20this%20is%20not%20an%20appropriate%20method.%20Any%20ideas%20are%20appreciated.%20Thank%20you%2C%20in%20advance!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1092957%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EFiles%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOneDrive%20for%20Business%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1093337%22%20slang%3D%22en-US%22%3ERe%3A%20Empty%20Corporate%20OneDrive%20Recycle%20Bins%20from%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1093337%22%20slang%3D%22en-US%22%3EUse%20CSOM%20API%20in%20your%20code%20or%20PnP%20cmdlets%20instead%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1093450%22%20slang%3D%22en-US%22%3ERe%3A%20Empty%20Corporate%20OneDrive%20Recycle%20Bins%20from%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1093450%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F60%22%20target%3D%22_blank%22%3E%40Juan%20Carlos%20Gonz%C3%A1lez%20Mart%C3%ADn%3C%2FA%3E%26nbsp%3BThank%20you.%20What%20would%20a%20script%20look%20like%20that%20empties%20both%20the%20regular%20and%20secondary%20recycle%20bin%20for%20the%20OneDrive%20account%20in%20question%3F%20Would%20the%20URL%20remain%20%60%5Btenant%5D-my.sharepoint.com%2Fpersonal%2F%5Busername%5D%60%3F%20The%20script%20I%20use%20for%20our%20SPO%20tenant%20looks%20like%20this%20(this%20one%20empties%20the%20regular%20recycle%20bin)%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3ETry%20%7B%20%20%20%20%0A%20%20%20%20%23Setup%20the%20context%0A%20%20%20%20%24Ctx%20%3D%20New-Object%20Microsoft.SharePoint.Client.ClientContext(%24SiteUrl)%0A%20%20%20%20%24Ctx.Credentials%20%3D%20%24Credentials%0A%20%20%20%20%20%0A%20%20%20%20%23Move%20All%20Deleted%20Items%20to%202nd%20Stage%20Recycle%20bin%0A%20%20%20%20%24Ctx.Site.RecycleBin.MoveAllToSecondStage()%0A%20%20%20%20%24Ctx.ExecuteQuery()%0A%7D%0Acatch%20%7B%0A%20%20%20%20write-host%20%22Error%3A%20%24(%24_.Exception.Message)%22%20-foregroundcolor%20Red%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EHow%20can%20I%20modify%20this%20for%20a%20user's%20OneDrive%3F%20Thanks%20again!%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
New 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
Highlighted
Best Response confirmed by Charisma Riley (New Contributor)
Solution
Use CSOM API in your code or PnP cmdlets instead
Highlighted

@Juan Carlos González Martín 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!