How To Bulk Delete Document Library With Powershell

Msdc 271 Reputation points

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


 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
        $listItems = $list.GetItems($query)
        $query.ListItemCollectionPosition = $listItems.ListItemCollectionPosition

        foreach ($item in $listItems)
            Write-Host "Deleting Item - $($item.Id)" -ForegroundColor Yellow
    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.
1,814 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,441 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,513 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Amos Wu-MSFT 4,041 Reputation points

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

    #Get Web and List objects  
    $web = Get-SPWeb -site $WebURL  
    $list = $web[0].Lists[$LibraryName]  
    #delete sharepoint document library using powershell  

    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

    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?


    0 comments No comments