how to empty the recycle bin in SharePoint using PowerShell?

Henrietta Mueller 0 Reputation points
2024-03-17T10:53:24.86+00:00

Hi members, In SharePoint, I need to empty the recycle bin in SharePoint through PowerShell, but i was facing an error

"You cannot call a method on a null-valued expression."

error2

Anyone please suggest me how to empty the recycle bin in SharePoint using PowerShell?

Microsoft 365 and Office SharePoint Server For business
Microsoft 365 and Office SharePoint Development
Microsoft 365 and Office SharePoint For business Windows
Windows for business Windows Server User experience PowerShell
{count} votes

2 answers

Sort by: Most helpful
  1. Nikunj Kalyani 0 Reputation points
    2024-03-17T11:37:26.33+00:00

    Please try to use the below code to get the credential object.

    # Prompt the user to enter credentials

    $Credential = Get-Credential

    # Use the credentials to connect to SharePoint Online

    Connect-SPOService -URL https://tenant-admin.sharepoint.com -Credential $Credential


  2. Islam Elashry 0 Reputation points
    2024-03-17T11:40:15.6833333+00:00

    To empty the recycle bin in SharePoint using PowerShell, you can use the EmptyRecycleBin method of the RecycleBin class. Here's a simple script to accomplish this:

    # Load SharePoint PowerShell snap-in
    if ((Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null) {
        Add-PSSnapin Microsoft.SharePoint.PowerShell
    }
    # Specify the URL of the site
    $siteUrl = "https://your-sharepoint-site-url"
    # Connect to the SharePoint site
    $site = Get-SPSite $siteUrl
    # Get the recycle bin of the site
    $recycleBin = $site.RecycleBin
    # Empty the recycle bin
    $recycleBin | ForEach-Object { $recycleBin.Delete($_.ID) }
    # Dispose of the SharePoint site object
    $site.Dispose()
    Write-Host "Recycle bin emptied successfully."
    

    replace "https://your-sharepoint-site-url" with the URL of your SharePoint site.

    This script connects to your SharePoint site, retrieves the recycle bin, and then iterates through each item in the recycle bin, deleting them one by one. Finally, it displays a message indicating that the recycle bin has been emptied successfully. Make sure to run this script with appropriate permissions, as it requires sufficient rights to delete items from the recycle bin.


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.