Fix "List cannot be deleted while on hold or retention policy" Error on SharePoint Online

Problem:  When trying to delete a document library in SharePoint Online, it gave an error message "list cannot be deleted while on hold or retention policy."

list cannot be deleted while on hold or retention policy

Solution: 
As the error message says: List cannot be deleted while on hold or retention policy, this error is due to the site is under hold or retention policy. So, to delete the list you must delete all its files and sub-folders first. Otherwise, you should remove the retention policy under Office 365 compliance center. You can quickly check if the site is under hold by going to:
  • Admin centers >> Security & Compliance >> Search & investigation
  • Click on eDiscovery, check if you have created any case. >> If you see any case, click Open
  • Under Hold, Check if you see any site.  
Deleting files at each folder could be haunting especially when you have a deep folder structure. Fortunately, we've PowerShell to relive the burden.

PowerShell to Clear Document Library's Content and then Delete
This PowerShell script deletes all files, and then sub-folders and then the given document library in SharePoint Online.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Variables
$SiteURL = "https://crescent.sharepoint.com/Sites/Marketing"
$LibraryName = "Branding Templates"
 
#Function to Delete all files and Sub-folders of a given Folder
Function Empty-SPOFolder([Microsoft.SharePoint.Client.Folder]$Folder)
{
    Try {
        #Get All Files from the Folder
        $Files = $Folder.Files
        $Ctx.Load($Files)
        $Ctx.ExecuteQuery()
  
        #Iterate through each File in the folder
        Foreach($File in $Files)
        {
            #Delete the file
            $Folder.Files.GetByUrl($File.ServerRelativeUrl).Recycle() | Out-Null
            Write-host -f Green "Deleted File '$($File.Name)' from '$($File.ServerRelativeURL)'"
        }
        $Ctx.ExecuteQuery()
  
        #Process all Sub Folders of the given folder
        $SubFolders = $Folder.Folders
        $Ctx.Load($SubFolders)
        $Ctx.ExecuteQuery()
        Foreach($Folder in $SubFolders)
        {
            #Exclude "Forms" and Hidden folders
            If( ($Folder.Name -ne "Forms") -and (-Not($Folder.Name.StartsWith("_"))))
            {
                #Call the function recursively to empty the folder
                Empty-SPOFolder -Folder $Folder
 
                #Delete the folder
                $Ctx.Web.GetFolderById($Folder.UniqueId).Recycle() | Out-Null
                $Ctx.ExecuteQuery()
                Write-host  -f Green "Deleted Folder:"$Folder.ServerRelativeUrl
            }
        }
    }
    Catch {
    write-host -f Red "Error:" $_.Exception.Message
    }
}
 
Try {
    #Get Credentials to connect
    $Cred= Get-Credential
 
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
   
    #Get the Library
    $Library = $Ctx.web.Lists.GetByTitle($LibraryName)
    $RootFolder = $Library.RootFolder
    $Ctx.Load($Library)
    $Ctx.Load($RootFolder)
    $Ctx.executeQuery()

    #Call the function to empty Folder
    Empty-SPOFolder $RootFolder
 
    #Delete the Document Library
    $Library.Recycle() | Out-Null
    $Ctx.ExecuteQuery() 
    Write-host  -f Green "Deleted Document Library Successfully!"
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

Delete Document Library in Retention Hold using PnP PowerShell
Let's fix "list cannot be deleted while on hold or retention policy" error on SharePoint Online by deleting all files and folders first and then the document library using PnP PowerShell.
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$ListName = "Branding Templates"

#Connect to the Site
Connect-PnPOnline -URL $SiteURL -Credentials (Get-Credential)

#Get the web & document Library
$Web = Get-PnPWeb
$List = Get-PnPList -Identity $ListName

#Function to delete all Files and sub-folders from a Folder
Function Empty-PnPFolder([Microsoft.SharePoint.Client.Folder]$Folder)
{
    #Get the site relative path of the Folder
    $FolderSiteRelativeURL = $Folder.ServerRelativeUrl.Replace($web.ServerRelativeUrl,"")

    #Get All files in the folder
    $Files = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType File
    
    #Delete all files in the Folder
    ForEach ($File in $Files)
    {
        #Delete File
        Remove-PnPFile -ServerRelativeUrl $File.ServerRelativeURL -Force -Recycle
        Write-Host -f Green ("Deleted File: '{0}' at '{1}'" -f $File.Name, $File.ServerRelativeURL)        
    }

    #Process all Sub-Folders
    $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("_"))))
        {
            #Call the function recursively
            Empty-PnPFolder -Folder $SubFolder

            #Delete the folder
            Remove-PnPFolder -Name $SubFolder.Name -Folder $FolderSiteRelativeURL -Force -Recycle
            Write-Host -f Green ("Deleted Folder: '{0}' at '{1}'" -f $SubFolder.Name, $SubFolder.ServerRelativeURL)
        }
    }
}

#Get the Root Folder of the Document Library and call the function to empty folder contents recursively
Empty-PnPFolder -Folder $List.RootFolder

#Now delete the document library
Remove-PnPList -Identity $ListName -Recycle -Force
Write-host -f Green "Document Library Deleted Successfully!"
If just want to clear the document library, without deleting the library too, just comment the line#50 - Remove-PnPList.
Fix "List cannot be deleted while on hold or retention policy" Error on SharePoint Online Fix "List cannot be deleted while on hold or retention policy" Error on SharePoint Online Reviewed by Salaudeen Rajack on March 04, 2019 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.