Deleting file and folder from Preservation Hold Library

Aase Nomad 246 Reputation points
2022-02-14T21:13:46.997+00:00

I've this simple script and basically I'm trying to delete all the files and folder inside Prevention Hold Library for this user but I'm getting this error in the Website and in PowerShell.

This user used to have OneDrive on hold but I already remove the hold but still not sure why I am getting this error. I heard from my senior that there is 30 days Retention Policy so anybody know it might be why I'm getting this error?.

if so, is there a way to still remove it so that we can free up the space immediatly?.

   Connect-PnPOnline -Url https://company-my.sharepoint.com/personal/yzqpsn_nam_corp_gm_com -useWebLogin  
     
   Get-PnPListItem -List 'Preservation Hold Library' -PageSize 1 | ForEach-Object {  
     
       $Filename = $_.FieldValues.FileLeafRef  
     
       Write-Host "Removing $Filename" -ForegroundColor Cyan  
     
       Remove-PnPListItem -List 'Preservation Hold Library' -Identity $_ -Force  
     
   }  

174236-image.png

174143-image.png

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
11,230 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
3,557 questions
OneDrive Management
OneDrive Management
OneDrive: A Microsoft file hosting and synchronization service.Management: The act or process of organizing, handling, directing or controlling something.
1,443 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,628 questions
0 comments No comments
{count} votes

Accepted answer
  1. CaseyYang-MSFT 10,456 Reputation points
    2022-02-15T07:59:54.327+00:00

    Hi @Aase Nomad ,

    1.Please make sure all files are not checked out to another user.

    2.You could try to use following PowerShell commands:

    #Parameters  
    $SiteURL = "https://xxx.sharepoint.com/sites/xxx"  
    $ListName = "xxx"  
    
    #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  
        If($Folder.Context.web.ServerRelativeURL -eq "/")  
        {  
            $FolderSiteRelativeURL = $Folder.ServerRelativeUrl  
        }  
        Else  
        {         
            $FolderSiteRelativeURL = $Folder.ServerRelativeUrl.Replace($Folder.Context.web.ServerRelativeURL,[string]::Empty)  
        }  
    
        #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!"  
    

    For Reference: Fix “List cannot be deleted while on hold or retention policy” Error on SharePoint Online
    Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link.


    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.


2 additional answers

Sort by: Most helpful
  1. Nico P 1 Reputation point
    2022-06-24T15:46:04.297+00:00

    You are getting the errors because of the possible issues below:

    Hope this helps.

    0 comments No comments

  2. Owolabi 26 Reputation points
    2023-08-07T14:20:12.46+00:00

    I also found this to be useful though it is not a Microsoft doc

    https://www.sharepointdiary.com/2021/10/how-to-delete-files-from-preservation-hold-library.html

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.