How To Bulk Delete Document Library With Powershell

Msdc 271 Reputation points
2021-03-10T14:54:03.107+00:00

Hey SharePoint Fam,

I have a document library that I need to just totally remove that has 300k documents. Of course I am not able to delete one by one and was wondering if there is a script or tool I could use to simply delete all files or just delete the entire library all together.

I've tried following script but get runtime exception errors

Blockquote

 Add-PSSnapin Microsoft.SharePoint.Powershell -ea SilentlyContinue

    ##  SP URL
    Write-Host "Provide SharePoint URL:" -ForegroundColor Yellow
    $webURL = Read-Host
    $web = Get-SPweb "http://domain/subsite"

    ## LIST NAME
    Write-Host "Enter name of the list:" -ForegroundColor Yellow
    $listName = Read-Host
    $list = $web.lists["libraryname"]

    ## SET QUERY
    $query = New-Object Microsoft.SharePoint.SPQuery
    $query.ViewAttributes = "Scope='Recursive'"
    $query.RowLimit = 1000
    $query.ViewFields = "<FieldRef Name='ID'/>"
    $query.ViewFieldsOnly = $true


    ## EXECUTE
    do
    {
        $listItems = $list.GetItems($query)
        $query.ListItemCollectionPosition = $listItems.ListItemCollectionPosition

        foreach ($item in $listItems)
        {
            Write-Host "Deleting Item - $($item.Id)" -ForegroundColor Yellow
            $list.GetItemById($item.Id).delete()
        }
    }
    while ($null -eq $query.ListItemCollectionPosition)

    Write-Host "Script finished." -ForegroundColor Green
SharePoint Server
SharePoint Server
A family of Microsoft on-premises document management and storage systems.
2,206 questions
SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,569 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,794 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Amos Wu-MSFT 4,051 Reputation points
    2021-03-11T08:15:33.6+00:00

    You could delete the library in the library setting.
    76530-image.png
    And as I test, this powershell script works well.

    $WebURL="http://sp"  
    $LibraryName="docLib"  
       
    #Get Web and List objects  
    $web = Get-SPWeb -site $WebURL  
    $list = $web[0].Lists[$LibraryName]  
       
    #delete sharepoint document library using powershell  
    $list.Delete()  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Msdc 271 Reputation points
    2021-03-30T13:38:10.957+00:00

    I've tried the Delete This Document Library option but it does not work. I get a error: The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator.

    The document library currently has around 500k documents loaded. Would I need to temporarily adjust my threshold to 500k?

    Thanks

    0 comments No comments