GitHub Icon Image
GitHub

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.

  • PnP PowerShell
  • CLI for Microsoft 365

$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 ?


$siteUrl = "https://yourtenantname.sharepoint.com/sites/SiteCollection"
$libraryName = "YourLibraryName"

write-host $("Start time " + (Get-Date))

$m365Status = m365 status
if ($m365Status -match "Logged Out") {
    m365 login
}

#Remove all subfolders
$folders = m365 spo folder list --webUrl $siteUrl --parentFolderUrl $libraryName | ConvertFrom-Json
foreach($folder in $folders) {
    if(($folder.Name -ne "Forms") -and (-Not($folder.Name.StartsWith("_")))) {
        #Delete the folder
        m365 spo folder remove --webUrl $siteUrl --folderUrl $folder.ServerRelativeUrl --confirm
        Write-Host -f Green ("Deleted Folder: '{0}' at '{1}'" -f $folder.Name, $folder.ServerRelativeUrl)
    }
}

#Remove all files
$files = m365 spo file list --webUrl $siteUrl --folder $libraryName | ConvertFrom-Json
foreach($file in $files) {
    #Delete File
    m365 spo file remove --webUrl $siteUrl --url $file.ServerRelativeUrl --confirm
    Write-Host -f Green ("Deleted File: '{0}' at '{1}'" -f $file.Name, $file.ServerRelativeUrl)     
}

m365 spo list remove --webUrl $siteUrl --title $libraryName --confirm
Write-Host ("Library {0} deleted" -f $libraryName)

write-host $("End time " + (Get-Date))

Check out the CLI for Microsoft 365 to learn more at: https://aka.ms/cli-m365

Important changes coming to the way you login into CLI for Microsoft 365 (effective 9th September 2024) see Changes in PnP Management Shell registration in Microsoft 365

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.

Back to top Script Samples
Generated by DocFX with Material UI