The most efficient way to a lot of transaction in SharePoint is using of SPWeb.ProcessBatchData method. For deleting operations there are very helpfull post. Code exaple from that post:
// We prepare a String.Format with a String.Format, this is why we have a {{0}}
string command = String.Format("<Method><SetList Scope="Request">{0}</SetList><SetVar Name="ID">{{0}}</SetVar><SetVar Name="Cmd">Delete</SetVar><SetVar Name="owsfileref">{{1}}</SetVar></Method>", list.ID);
// We get everything but we limit the result to 100 rows
SPQuery q = new SPQuery();
q.RowLimit = 100;
// While there's something left
while (list.ItemCount > 0)
{
// We get the results
SPListItemCollection coll = list.GetItems(q);
StringBuilder sbDelete = new StringBuilder();
sbDelete.Append("<?xml version="1.0" encoding="UTF-8"?><Batch>");
Guid[] ids = new Guid[coll.Count];
for (int i=0;i<coll.Count;i++)
{
SPListItem item = coll[i];
sbDelete.Append(string.Format(command, item.ID.ToString(), item.File.ServerRelativeUrl));
ids[i] = item.UniqueId;
}
sbDelete.Append("</Batch>");
// We execute it
web.ProcessBatchData(sbDelete.ToString());
//We remove items from recyclebin
web.RecycleBin.Delete(ids);
list.Update();
}
}
string command = String.Format("<Method><SetList Scope="Request">{0}</SetList><SetVar Name="ID">{{0}}</SetVar><SetVar Name="Cmd">Delete</SetVar><SetVar Name="owsfileref">{{1}}</SetVar></Method>", list.ID);
// We get everything but we limit the result to 100 rows
SPQuery q = new SPQuery();
q.RowLimit = 100;
// While there's something left
while (list.ItemCount > 0)
{
// We get the results
SPListItemCollection coll = list.GetItems(q);
StringBuilder sbDelete = new StringBuilder();
sbDelete.Append("<?xml version="1.0" encoding="UTF-8"?><Batch>");
Guid[] ids = new Guid[coll.Count];
for (int i=0;i<coll.Count;i++)
{
SPListItem item = coll[i];
sbDelete.Append(string.Format(command, item.ID.ToString(), item.File.ServerRelativeUrl));
ids[i] = item.UniqueId;
}
sbDelete.Append("</Batch>");
// We execute it
web.ProcessBatchData(sbDelete.ToString());
//We remove items from recyclebin
web.RecycleBin.Delete(ids);
list.Update();
}
}
[System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
[System.Reflection.Assembly]::Load("Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
[System.Reflection.Assembly]::Load("Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
[System.Reflection.Assembly]::Load("System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
write-host
# Enter your configuration here
$siteUrl = "http://mysharepointsite.example.com/"
$listName = "Name of my list"
$batchSize = 1000
write-host "Opening web at $siteUrl..."
$site = new-object Microsoft.SharePoint.SPSite($siteUrl)
$web = $site.OpenWeb()
write-host "Web is: $($web.Title)"
$list = $web.Lists[$listName];
write-host "List is: $($list.Title)"
while ($list.ItemCount -gt 0)
{
write-host "Item count: $($list.ItemCount)"
$batch = "<?xml version=`"1.0`" encoding=`"UTF-8`"?><Batch>"
$i = 0
foreach ($item in $list.Items)
{
$i++
write-host "`rProcessing ID: $($item.ID) ($i of $batchSize)" -nonewline
$batch += "<Method><SetList Scope=`"Request`">$($list.ID)</SetList><SetVar Name=`"ID`">$($item.ID)</SetVar><SetVar Name=`"Cmd`">Delete</SetVar><SetVar Name=`"owsfileref`">$($item.File.ServerRelativeUrl)</SetVar></Method>"
if ($i -ge $batchSize) { break }
}
$batch += "</Batch>"
write-host
write-host "Sending batch..."
# We execute it
$result = $web.ProcessBatchData($batch)
write-host "Emptying Recycle Bin..."
# We remove items from recyclebin
$web.RecycleBin.DeleteAll()
write-host
$list.Update()
}
write-host "Done."
[System.Reflection.Assembly]::Load("Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
[System.Reflection.Assembly]::Load("Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
[System.Reflection.Assembly]::Load("System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
write-host
# Enter your configuration here
$siteUrl = "http://mysharepointsite.example.com/"
$listName = "Name of my list"
$batchSize = 1000
write-host "Opening web at $siteUrl..."
$site = new-object Microsoft.SharePoint.SPSite($siteUrl)
$web = $site.OpenWeb()
write-host "Web is: $($web.Title)"
$list = $web.Lists[$listName];
write-host "List is: $($list.Title)"
while ($list.ItemCount -gt 0)
{
write-host "Item count: $($list.ItemCount)"
$batch = "<?xml version=`"1.0`" encoding=`"UTF-8`"?><Batch>"
$i = 0
foreach ($item in $list.Items)
{
$i++
write-host "`rProcessing ID: $($item.ID) ($i of $batchSize)" -nonewline
$batch += "<Method><SetList Scope=`"Request`">$($list.ID)</SetList><SetVar Name=`"ID`">$($item.ID)</SetVar><SetVar Name=`"Cmd`">Delete</SetVar><SetVar Name=`"owsfileref`">$($item.File.ServerRelativeUrl)</SetVar></Method>"
if ($i -ge $batchSize) { break }
}
$batch += "</Batch>"
write-host
write-host "Sending batch..."
# We execute it
$result = $web.ProcessBatchData($batch)
write-host "Emptying Recycle Bin..."
# We remove items from recyclebin
$web.RecycleBin.DeleteAll()
write-host
$list.Update()
}
write-host "Done."