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?

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,715 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,980 questions
SharePoint Server Management
SharePoint Server Management
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Management: The act or process of organizing, handling, directing or controlling something.
2,948 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,526 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,526 questions
{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.