【问题标题】:PowerShell Delete Large SharePoint List item 14 days oldPowerShell 删除 14 天前的大型 SharePoint 列表项
【发布时间】:2021-01-15 17:22:45
【问题描述】:

下面的脚本适用于一个小型共享点列表,但我似乎无法让它适用于超过 700,000 行的大型列表。

任何建议将不胜感激。

$list = "Audit Only Test"
$items = Get-PnPListItem -List $listName -Fields "Title","Created","ID","ShiftDate"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin
   
#Define Query to get list items
$Query= "<View Scope='RecursiveAll'>
          <Query>
                <Where>
                    <And>
                        <Lt>
                            <FieldRef Name='ShiftDate' Type='DateTime'/>
                            <Value Type='DateTime' IncludeTimeValue='TRUE'>
                                <Today OffsetDays='-7'/>
                            </Value>
                        </Lt>
                        <Eq>
                            <FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value>
                        </Eq>
                    </And>
                </Where>
            </Query>
         <RowLimit>500</RowLimit>
        </View>"
 
#Get All Items from the List in batches
$ListItems = Get-PnPListItem -List $ListName -Query $Query -PageSize 500

#Write-host "Total Number of Items Found:"$ListItems.count
foreach($item in $listItems)
{
 Write-Host "Deleting Item - $($item.Id)"
 #$item.deleteobject()
 Remove-PnPListItem -List $listName -Identity $item.Id -Force -ErrorAction Stop
 }
 Write-Host -f Green "`nAll List Items Deleted Successfully!"   ``` 

【问题讨论】:

  • 如果您正在执行行删除,我建议您只检索匹配过滤器实际需要的列并执行删除。在您的示例中,它将是删除的 ID 字段和过滤器的 ShiftDate 字段:$items = Get-PnPListItem -List $listName -Fields "ID","ShiftDate"
  • 列表中有 700,000 个项目,我希望 ShiftDate 在超过 5,000 个项目之前声明一个索引,否则性能会非常缓慢。

标签: sharepoint


【解决方案1】:

您可以使用以下命令删除项目:

Get-PnPListItem -List $ListName -Query $Query -PageSize 500 -ScriptBlock { Param($items)} | % { Remove-PnPListItem -List $ListName -Identity $_.Id -Force}

更新:

您不必使用 for 每个循环遍历项目,只需使用如下代码即可:

Connect-PnPOnline -Url $SiteURL -UseWebLogin   
#Define Query to get list items
$Query= "<View Scope='RecursiveAll'>
          <Query>
                <Where>
                    <And>
                        <Lt>
                            <FieldRef Name='ShiftDate' Type='DateTime'/>
                            <Value Type='DateTime' IncludeTimeValue='TRUE'>
                                <Today OffsetDays='-7'/>
                            </Value>
                        </Lt>
                        <Eq>
                            <FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value>
                        </Eq>
                    </And>
                </Where>
            </Query>
         <RowLimit>500</RowLimit>
        </View>"
$ListName="Yourlist name"
Get-PnPListItem -List $ListName -Query $Query -PageSize 10 -ScriptBlock { Param($items)} | % {
Write-Host "Deleting Item - $($_.Id)"
 Remove-PnPListItem -List $ListName -Identity $_.Id -Force}   
 Write-Host -f Green "`nAll List Items Deleted Successfully!"   ```  

【讨论】:

  • 看起来它仍然只运行一次,而不是循环遍历整个列表。查询后添加的更新代码:#Write-host "Total Number of Items Found:"$ListItems.count foreach($item in $listItems) { Write-Host "Deleting Item - $($item.Id)" #$item.deleteobject() Get-PnPListItem -List $ListName -Query $Query -PageSize 500 -ScriptBlock { Param($items)} | % { Remove-PnPListItem -List $ListName -Identity $_.Id -Force} } Write-Host -f Green "`nAll List Items Deleted Successfully!"
  • 您不必使用 foreach 来循环整个列表。请检查我更新的答案。只需使用适合您的代码即可。
猜你喜欢
  • 1970-01-01
  • 2011-07-29
  • 2014-07-15
  • 1970-01-01
  • 2021-03-23
  • 1970-01-01
  • 1970-01-01
  • 2014-04-26
  • 2017-09-16
相关资源
最近更新 更多