当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 修复 SharePoint Online 上的“处于保留或保留策略状态时无法删除列表”错误

作者:精品下载站 日期:2024-12-14 21:16:48 浏览:15 分类:玩电脑

修复 SharePoint Online 上的“处于保留或保留策略状态时无法删除列表”错误


问题:尝试删除 SharePoint Online 中的文档库时,出现错误消息:“列表在保留或保留策略期间无法删除。”

[玩转系统] 修复 SharePoint Online 上的“处于保留或保留策略状态时无法删除列表”错误

解决方案:

正如错误消息所示:在保留或保留策略期间无法删除列表。此错误是由于该网站处于保留或保留政策之下。因此,要删除该列表,您必须先删除其所有文件和子文件夹。否则,您应该删除 Office 365 合规中心下的保留策略。您可以通过以下方式快速检查网站是否被冻结:

  1. 管理中心 >> 安全与合规 >> 搜索与调查
  2. 单击“电子数据展示”,然后检查您是否已创建任何案例 >> 如果您看到任何案例,请单击“打开”
  3. 在“保留”下,检查是否看到任何站点。

删除每个文件夹中的文件可能会令人难以忘怀,尤其是对于深层文件夹结构。幸运的是,我们有 PowerShell 来减轻负担。

PowerShell清除文档库的内容然后删除

此 PowerShell 脚本将删除 SharePoint Online 中的所有文件、子文件夹和给定文档库。


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\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
}
使用“文件资源管理器”视图一次性删除 SharePoint Online 文档库中的所有文件和文件夹!

使用 PnP PowerShell 删除保留保留中的文档库

让我们通过先删除所有文件和文件夹,然后使用 PnP PowerShell 删除文档库来修复 SharePoint Online 上的“在保留或保留策略时无法删除列表”错误。请注意,如果文件已签出,则无法删除它!必须将其签入才能删除。 PowerShell 批量签入 SharePoint Online 中的所有文档


#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
    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
            $ParentFolderURL = $FolderSiteRelativeURL.TrimStart("/")
            Remove-PnPFolder -Name $SubFolder.Name -Folder $ParentFolderURL -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!"

如果您只想清除文档库而不删除库,请注释行#50 - Remove-PnPList。这是关于从文档库中删除所有文件和文件夹的另一篇文章:SharePoint Online:如何使用 PowerShell 删除文档库中的所有文件?

PnP PowerShell 清空 SharePoint Online 文档库

上面的脚本适用于小于 5000 个文件的较小库。但在较大的库上,它会收到“Get-PnPFolderItem:尝试的操作被禁止,因为它超出了列表视图阈值。”错误,因为 Get-PnPFolderItem 无法处理大型列表和库。因此,这是通过使用 PnP 批处理方法更快地删除文档库中的所有文件和文件夹来清空文档库的备用脚本:


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Inventory"
$ListName = "Branding Templates"
 
Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive
    $List =  Get-PnPList $ListName
    $ItemCount =  $List.ItemCount
    If($ItemCount -eq 0) { Write-host "Document Library is Already Empty!" -f Yellow; Break}
 
    #Get All Items from the Library
    $global:counter = 0
    $ListItems = Get-PnPListItem -List $ListName -PageSize 500 -Fields ID -ScriptBlock { Param($items) $global:counter += $items.Count; Write-Progress -PercentComplete `
            ($global:Counter / $ItemCount * 100) -Activity "Getting Items from List" -Status "Getting Items $global:Counter of $($ItemCount)"}
    Write-Progress -Activity "Completed Getting Items from Library $($List.Title)" -Completed
 
    #Sort List Items based on Server Relative URL - Descending
    $SortedItems = $ListItems | Select-Object @{Name="ServerRelativeURL";Expression={$_.FieldValues.FileRef}}, ID | Sort-Object -Descending -Property ServerRelativeURL
     
    #Delete List Items in the Batch
    $Batch = New-PnPBatch
    ForEach ($Item in $SortedItems)
    {
        Remove-PnPListItem -List $ListName -Identity $Item.ID -Recycle -Batch $Batch
        #Write-host "Deleted Item:"$Item.ServerRelativeURL
    }
    Invoke-PnPBatch -Batch $Batch -Details
 
    Write-host -f Green "All Items in the Library deleted Successfully!"
 }
Catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯