none
Delete list items after CAML Query RRS feed

  • Question

  • I want to delete list items from my CAML query but currently cannot using a foreach loop. What would be the best way to delete the items after the CAML query?

    $Query = New-Object Microsoft.SharePoint.SPQuery
                $Query.Query = "
                          <Where>
                           <Leq>
                            <FieldRef Name='Created' />
                             <Value Type='DateTime'>
                               <Today OffsetDays='-30' />
                             </Value>
                           </Leq>
                          </Where>
                            <OrderBy>
                              <FieldRef Name='Created' Ascending='False' />
                           </OrderBy>"
    
                #Get List Items matching the query
                $ListItems = $oList.GetItems($Query)
    
                write-host "Number of items retrieved:" $ListItems.Count;
    
    
                foreach($ListItem in $ListItems)
                {
                    write-host "Deleting"+ $listItem.ID + $listItem.Name + $listItem["Created"];
                    #$ListItem.delete();
    
                }

    Friday, December 13, 2019 1:35 AM

Answers

  • Hi,

    We can use the "for" loop to achieve it.

    #Loop through each item and delete
    For ($i = $ListItems.Count-1; $i -ge 0; $i--)
    {
    	write-host "Deleting"+ $listItem.ID + $listItem.Name + $listItem["Created"];
    	$ListItems[$i].delete()
    }

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.


    Friday, December 13, 2019 6:36 AM
    Moderator

All replies

  • I want to delete list items from my CAML query but currently cannot using a foreach loop. What would be the best way to delete the items after the CAML query?

    $Query = New-Object Microsoft.SharePoint.SPQuery
                $Query.Query = "
                          <Where>
                           <Leq>
                            <FieldRef Name='Created' />
                             <Value Type='DateTime'>
                               <Today OffsetDays='-30' />
                             </Value>
                           </Leq>
                          </Where>
                            <OrderBy>
                              <FieldRef Name='Created' Ascending='False' />
                           </OrderBy>"
    
                #Get List Items matching the query
                $ListItems = $oList.GetItems($Query)
    
                write-host "Number of items retrieved:" $ListItems.Count;
    
    
                foreach($ListItem in $ListItems)
                {
                    write-host "Deleting"+ $listItem.ID + $listItem.Name + $listItem["Created"];
                    #$ListItem.delete();
    
                }

    $ListItems|%{$list.GetItemById($_.Id).Delete()}  

    Please remember to mark the replies as answers if they helped.
    Linyu - SharePoint Engineer
    QQ Group: 72637444

    Friday, December 13, 2019 5:59 AM
  • Hi, it seems that you're trying to delete lists items that were create more than 30 days ago and run it by schedule. If you looking for a no code solution you my try use 3-rd party tool fro that called www.listman.io. It's was specifically designed for auto archive, export SharePoint list items with attachments, may be run as a windows service and supports CRON scheduling.
    Friday, December 13, 2019 6:30 AM
  • Hi,

    We can use the "for" loop to achieve it.

    #Loop through each item and delete
    For ($i = $ListItems.Count-1; $i -ge 0; $i--)
    {
    	write-host "Deleting"+ $listItem.ID + $listItem.Name + $listItem["Created"];
    	$ListItems[$i].delete()
    }

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.


    Friday, December 13, 2019 6:36 AM
    Moderator
  • Hi Dennis,

    Thanks for your help. This is working.

    Friday, December 13, 2019 6:51 AM