question

msdc avatar image
0 Votes"
msdc asked msdc commented

How To Bulk Delete Document Library With Powershell

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

office-sharepoint-server-administrationoffice-sharepoint-server-developmentoffice-sharepoint-server-itpro
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AmosWu-MSFT avatar image
0 Votes"
AmosWu-MSFT answered msdc commented

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.


image.png (32.4 KiB)
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @msdc,
Do you have any update on this issue?

0 Votes 0 ·

Hi @msdc,
I am checking to see how things are going there on this issue. Please let us know if you would like further assistance.
You can accept the helpful post as answer to help other community members find helpful information quickly.
You can also share your own solution here and accept it as answer, and we can learn from each other.

0 Votes 0 ·

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 Votes 0 ·
msdc avatar image
0 Votes"
msdc answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.