The process of building custom applications and tools that interact with Microsoft SharePoint, including SharePoint Online in Microsoft 365.
Hi @Somsak Narong
To bulk delete SharePoint document libraries, you’ll first need to connect to your site using PnP PowerShell. Follow the official guide here: Register an Entra ID Application to use with PnP PowerShell | PnP PowerShell.
When you reach the step in the article for assigning permissions, choose either AllSites.Manage or AllSites.FullControl to ensure you can manage libraries with PnP commands. For a smoother setup experience, it’s recommended to use PowerShell 7.5.
1.Delete all on-system document libraries at once.
Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/yoursite" -Interactive -ClientID <your application ID>
$ExcludedLibraries = @(
"Converted Forms","Master Page Gallery","Customized Reports","Form Templates",
"Site Collection Documents","Site Collection Images","Reporting Templates",
"Solution Gallery","Style Library","Web Part Gallery",
"Site Assets","wfpub","Site Pages","Images","Documents"
)
# Get all document libraries (excluding system ones)
$Libraries = Get-PnPList | Where-Object {
$_.BaseType -eq "DocumentLibrary" -and
$_.Hidden -eq $false -and
$_.IsSystemList -ne $true -and
$_.IsCatalog -eq $false -and
$ExcludedLibraries -notcontains $_.Title
}
# Delete each library safely
ForEach($Library in $Libraries) {
try {
Remove-PnPList -Identity $Library -Recycle -Force -ErrorAction Stop
Write-Host "Library '$($Library.Title)' Deleted Successfully!" -ForegroundColor Green
}
catch {
Write-Host "Skipped '$($Library.Title)' - $($_.Exception.Message)" -ForegroundColor Yellow
}
}
2.Delete Libraries Based on a CSV List
Prepare a CSV file with a column named LibraryName listing the libraries you want to delete.
| LIbraryName | |
|---|---|
| Library A | |
| Library B |
$CSVFilePath = "C:\folder\BulkDeleteLibraries.csv"
Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/yoursite" -Interactive -ClientID <your application ID>
Import-Csv $CSVFilePath | ForEach-Object {
try {
Remove-PnPList -Identity $_.LibraryName -Recycle -Force -ErrorAction Stop
Write-Host "Deleted: $($_.LibraryName)" -ForegroundColor Green
}
catch {
Write-Host "Skipped or Failed: $($_.LibraryName)" -ForegroundColor Yellow
}
}
I hope this helps.
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
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.