0

I've got a SharePoint with a view called Retention that has around half a million entries, I want to delete the entries in the view from SharePoint as they are no longer required but I've not been able to find a quick way of doing this using PowerShell.

I've found this script and though it works it is incredibly slow, almost an hour to delete 3000 items;

Add-PSSnapin Microsoft.SharePoint.PowerShell

Function Cleardown
{
     #Add-PSSnapin Microsoft.SharePoint.PowerShell   

    # Get SharePoint list
    $web = Get-SPWeb "SP Web Address"
    $list = $web.Lists["List Name"]
    $view = $list.Views["View Name"]
    $items = $list.GetItems($view)
    $items.count

    # Loop through view
    for ($i=$items.count-1; $i -ge 0; $i--)
    { 
       $item = $items[$i]; 
       $item.Recycle(); 
       Write-host -NoNewLine "$i        `r"
    }
    $Web = ""
}

For ($loop=1; $loop -le 1; $loop++) #967
{

    write-host "Pass " $loop
    Cleardown  
}

Any suggestions for a way of doing this?

Thank you.

1 Answer 1

0

You should consider using batches or scriptBlock in PowerShell to move forward faster. You should expect 7-10 times faster item deletion with that.

I would suggest to use PNP PowerShell for that. See script example bellow using batches:

#Parameters
$SiteURL="https://tenant.sharepoint.com/sites/siteABC"
$ListName="ListName"
$ViewName= "ViewName"
 
#Connect to the Site        
Connect-PnPOnline -Url $SiteURL -Interactive 

#Get List
$List =  Get-PnPList -Identity $ListName 
#Get List View
$ListView  = Get-PnPView -List $ListName -Identity $ViewName -Includes ListViewXml
 

#Get List Items from the view
$listItems = Get-PnPListItem -List Tasks -Query $ListView.ListViewXml
#Create Batch
$batch = New-PnPBatch 
$listItems | Foreach-Object { Remove-PnPListItem -List $list -Identity $_ -Batch $batch } 
Invoke-PnPBatch -Batch $batch
1
  • OP is using the server snap-in. PnP is almost certainly going to run much slower than that. Commented Oct 19, 2023 at 21:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.