0

I have a list which consist of more than 5600 items. In my application i will search all the items from particular date range by using SPquery. As the result of the SPQuery, the splistitem collection have 2500 records. When i am trying to delete these items one by one. It will stop after particular numberof records deletion. It will not occur in a single stretch. I think this may be the one bug of Sharepoint. Does anyone faced the same issue early? If you guys have any solution for this please help me. I strucked on this issue. Thanks in advance.

  • Can you show the code you are using to delete the listitems? Are there any errors in the ULS logs when the deletion is occurring? – Jason Rastovski Jun 18 '13 at 14:08
0

I have refered this Useful Link to resolve my issue. I have used SPWeb.ProcessBatchData() method for deleting all records from my List.

0

I guess you might be using the following code:

SPListItemCollection delItemCol = customList.GetItems(spQuery);

for(int count=0; count< delItemCol.Count ;count++) delItemCol[count].delete();

Now here we can't use the foreach because if you try to modify the collection of SPListItemCollection, it will throw exception. So best is to use the for loop.

If you analyse the counter and delItemCol.Count.

        counter      delItemCol.Count
        0           20
        1           19
        2           18
        3           17
        4           16
        5           15
        6           14
        7           13
        8           12
        9           11
        10          10
        11           9

So...after sometime the value of delItemCol.Count is less than counter and will throw exception.

So how to deal with this in simplest way. Either use double for loop a or ProcessBatchData. This is not a manual code without any kind of testing. But it must work.

0

You loop should go in reverse order .... i.e.

for(int count=delItemCol.Count-1; count > delItemCol.Count ;count--) 
{
delItemCol[count].delete();
}
0

From my blog:

There is a virtual situation :

You have no archival / expiration policies setup for your list and on and off you want to delete everything older than 30 days . What you will do ?

This sample power shell solves your purpose and fulfill example for offset parameter in powershell :

$WebURL = "http://mysitecollection:100/myWeb";
$spWeb = Get-SPWeb -Identity $WebURL;

$sList = $spWeb.GetList("/myWeb/Lists/myList");
write-host $sList.Title;
$camlQuery = "<Where><Lt><FieldRef Name='Modified' /><Value Type='DateTime'><Today OffsetDays=-30 /></Value></Lt></Where>";
$spQuery = new-object Microsoft.SharePoint.SPQuery ;
$spQuery.Query = $camlQuery ;
$spQuery.ViewFields = "<FieldRef Name='ID' />";

$ToBeDeleted = $sList.GetItems() ;
$ToBeDeleted = $sList.GetItems($spQuery) ;
$i=0;
$ToBeDeleted | ForEach-Object {
    $i++;
    Write-Host $_.ID -foregroundcolor cyan

    $deaditem= $sList.GetItemById($_.ID);  # if you wish you may decide to recycle instead to recycle bin

    $deaditem.Delete();
}

write-host $i;

Ref :

Offset Today in SPQuery Example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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