Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PowerShell Delete Large SharePoint List item 14 days old

Status
Not open for further replies.

JimLes

IS-IT--Management
Feb 27, 2006
119
US
The script below works for a small SharePoint list but I cannot seem to get it to work with a large list over 900,000 rows.

Any suggestions would be greatly appreciated. I am very new to PowerShell but I think I am missing the loop.



$SiteName = "$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='-14'/>
</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!"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top