There is a trick to delete the versions from a SharePoint document library.
each time a version is deleted from the document library, item index will be changed. So, the version item collection gets flushed for each and every delete. To deal with this item Index change, we need to itereate through all the folders and files and make sure to delete the first indexed file always(file.Versions[0].Delete()). This makes sure that the lowest indexed item will be deleted for each and every index flush.
SPSite site = new SPSite("http://servername/sites/sitecollname");SPWeb web = site.OpenWeb();web.AllowUnsafeUpdates = true;SPDocumentLibrary docLib=(SPDocumentLibrary)web.Lists["Documents"];foreach (SPListItem folder in docLib.Folders){for (int i = 0; i < folder.Folder.Files.Count; i++){
SPFile file = folder.Folder.Files[i];
int counter = file.Versions.Count;
for (int j = 0; j < counter; j++)
{
if (file.Versions[0] != null)
{
file.Versions[0].Delete();
}
}
}}
web.Update();
web.AllowUnsafeUpdates = false;

W
ReplyDeleteHey, Umakanth :) I'm experiencing problems with that in Vista in my laptop, but the XP pc works fine. Do you know what should I do?
ReplyDelete