2

Hey SharePoint Ninjas,

I have a SharePoint 2010 list that has approx 9000 items. Now, each item has 4000+ versions (crazy I know)

I cannot rely on normal foreach loop in powershell to delete it. It is taking way too longer to delete the versions. I tried changing the versioning settings to "No versions" but to no success. The versions are still present there and consuming storage.

Any suggestions on how to tackle this?

Here is the code I am using -

$SPWeb = Get-SPWeb "http://thatsiteagain.sharepoint.com"
$myList = $SPWeb.Lists["My Crazy List"]


$itemColl = $myList.Items
foreach ($myItem in $itemColl)
{
    $versionsToDelete = $myItem.Versions
    $verCount = $versionsToDelete.Count
    for ($i = $verCount-1; $i -ge 0 ; $i--)
    {
        if (!$versionsToDelete[$i].IsCurrentVersion)
            { 
                $versionsToDelete[$i].Delete()
            }
     }
}
  • Can you please post your code here, so that accordingly we shall help you. – Karthik Jaganathan Feb 27 at 10:55
0
  1. Change the versioning settings to "Ni versions"
  2. Update each list item

Changing of versioning settings will take effect only after list item editing.

| improve this answer | |
0

Try the below code, which deletes the all versions of an item at a time. Without looping through every item in the list, we will not know whether an item has version history or not, so obviously we need to loop through the items in the list and check for version items and delete it.

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
#get site
$site = new-object Microsoft.SharePoint.SPSite("http://example.com/sitecollection/site")
# get web
$web = site.OpenWeb("website")
$list = $web.Lists["List Name"]

foreach ($item in $list.Items)
{
    if ($item.Versions.Count -gt 1)
    {   
        # delete all versions
        $item.Versions.DeleteAll()
    }     
}

$web.Dispose();
$site.Dispose();
| improve this answer | |
  • Thank you for the answer Karthik, but I am using list items and not library files. Any way I can tweak your code and use it? – Satyajit Paul Feb 27 at 11:26
  • I have updated with the changes and tested against a SharePoint list – Karthik Jaganathan Feb 27 at 12:28
0

@Karthik is on the right track, but accessing $list.Items on a list with 9000+ items is likely to kill the memory on your server, and potentially may just error out if your list view threshold is still at the default.

Instead, process items in batches and only get subset of the items using Microsoft.SharePoint.Client.CamlQuery and GetItems. You can help yourself a little bit by filtering out anything whose version is still "0.1".

Give this a try:

$web = Get-SPWeb http://yoursite.sharepoint.com
$list = $web.Lists["YourList"]

$spQuery = New-Object Microsoft.SharePoint.SPQuery
$spQuery.ViewAttributes = "Scope='Recursive'";
$spQuery.RowLimit = 1000
$caml = '<Where><Geq><FieldRef Name="_UIVersionString" /><Value Type="Text">0.1</Value></Geq></Where><OrderBy Override="TRUE"><FieldRef Name="ID"/></OrderBy>' 
$spQuery.Query = $caml 

do
{
    $listItems = $list.GetItems($spQuery)
    $spQuery.ListItemCollectionPosition = $listItems.ListItemCollectionPosition
    foreach($item in $listItems)
    {
        if ($item.Versions.Count -gt 1)
        {   
            # delete all versions
            $item.Versions.DeleteAll()
        }   
    }
}
while ($spQuery.ListItemCollectionPosition -ne $null)

$web.dispose();
| improve this answer | |

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.