How to delete SharePoint list items before specific date in SharePoint online using Powershell?

Hello SharePointers,

Below are the Pnp Powershell script to delete SharePoint list items before specific date in SharePoint online using Powershell.

Connect-PnPOnline -Url <yourtenant>.sharepoint.com

$listName = "EMployee"
$items = Get-PnPListItem -List $listName -Fields "Title","Created","ID","GUID"

// 30 days is the cut off period 
$days = (Get-Date).AddDays(-30)

foreach($item in $items) 
{
    $created = $item["Created"]

    Write-Host "Item created on $created"
    if($created -gt $days) 
    {
        continue;
    }

    try
    {
        Remove-PnPListItem -List $listName -Identity $item.Id -Force -ErrorAction Stop
    }
    catch
    {
        Write-Host "Unable to process delete$($item.Id) in list $listName"
    }
}

Happy SharePointing 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *