Delete a library exceeding the list threshold limit. Remove the files and folders before deleting the library.
Summary
Trying to delete a library exceeding the list view threshold results in the message "The attempted operation is prohibited because it exceeds the list view threshold" from the UI and using the cmdlet Remove-PnPList. The script was tested deleting a library containing more than 113 k files/nested folders.
PnP PowerShell
Implementation
- Open Windows PowerShell ISE
- Create a new file
- Copy a script below
Warning
Please be aware this script contains a command that will remove or delete an artifact, ensure you test and understand the implications of running the script.
$SiteURL = "https://yourtenantname.sharepoint.com/sites/SiteCollection"
$LibraryName = "YourLibraryName"
$ErrorActionPreference="Stop"
write-host $("Start time " + (Get-Date))
Connect-PnPOnline -URL $SiteURL -Interactive
#Get Root folder of the library
$Library = Get-PnPList -Identity $LibraryName -Includes RootFolder
$Folder = $Library.RootFolder
#Get the site relative path of the Folder
If($Folder.Context.web.ServerRelativeURL -eq "/")
{
$FolderSiteRelativeURL = $Folder.ServerRelativeUrl
}
Else
{
$FolderSiteRelativeURL = $Folder.ServerRelativeUrl.Replace($Folder.Context.web.ServerRelativeURL,[string]::Empty)
}
#Remove all files
$Files = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType File
ForEach ($File in $Files)
{
#Delete File
Remove-PnPFile -ServerRelativeUrl $File.ServerRelativeURL -Force
Write-Host -f Green ("Deleted File: '{0}' at '{1}'" -f $File.Name, $File.ServerRelativeURL)
}
#Remove all subfolders
$SubFolders = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType Folder
Foreach($SubFolder in $SubFolders)
{
#Exclude "Forms" and Hidden folders
If(($SubFolder.Name -ne "Forms") -and (-Not($SubFolder.Name.StartsWith("_"))))
{
#Delete the folder
Remove-PnPFolder -Name $SubFolder.Name -Folder $FolderSiteRelativeURL -Force
Write-Host -f Green ("Deleted Folder: '{0}' at '{1}'" -f $SubFolder.Name, $SubFolder.ServerRelativeURL)
}
}
Remove-PnPList -Identity $LibraryName -Force
Write-Host ("Library {0} deleted" -f $LibraryName)
write-host $("End time " + (Get-Date))
Check out the PnP PowerShell to learn more at: https://aka.ms/pnp/powershell
The way you login into PnP PowerShell has changed please read PnP Management Shell EntraID app is deleted : what should I do ?
Contributors
Author(s) |
---|
Reshmee Auckloo |
Adam Wójcik |
Disclaimer
THESE SAMPLES ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.