locked
How to delete document versions in sharepoint 2007 RRS feed

  • Question

  • Hey,

    i want to delete document versions from all document libraries in a site collection.

    i know we can restrict versions at a document library level , do we have such thing at site collection level?

     

    Thanks In Advance

     

     


    Share Knowledge and Spread Love!
    Friday, April 8, 2011 7:08 PM

Answers

  • How's this?  It's a quick PowerShell script.

    [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
    # get site
    $site = new-object Microsoft.SharePoint.SPSite("http://mossdev:8000")
    # loop through webs
    foreach ($web in $site.AllWebs)
    {
      # loop through all lists in web
      foreach ($list in $web.Lists)
      {
        # examine if BaseType of list is NOT a Document Library
        if ($list.BaseType -ne "DocumentLibrary") 
        {
          # forget the rest and return to top
          continue
        }
        # loop through each item
        foreach ($item in $list.Items)
        {
          # get the file, as we need to work with the file as we're in a document library
          $file = $item.File
          # delete all versions
          $file.Versions.DeleteAll()
        }
      }
    }
    $web.Dispose();
    $site.Dispose();
    

    Hope that helps

    - M


    - Michael Mukalian - 2010 MS MVP SharePoint Services - MCTS: MOSS 2007 Configuration - http://www.mukalian.com/blog
    • Marked as answer by David HM Monday, April 11, 2011 1:58 AM
    Monday, April 11, 2011 1:17 AM

All replies

  • Unfortunately there isn't any OOB tool to do that in 2007. Very frustrating when you hundreads of libraries to adjust manually one at a time!

    Just as a FYI - Most polular SharePoint admin tools (Axceler/ AvePoint) has that ability to do this as well.

    Hope this helps!


    BlueSky2010
    • Edited by Mike Walsh FIN Saturday, April 9, 2011 7:28 AM 2010 references removed. Please avoid referring to SP 2010 solutions (etc.) in pre-SP 2010 forums
    Friday, April 8, 2011 9:27 PM
  • How's this?  It's a quick PowerShell script.

    [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
    # get site
    $site = new-object Microsoft.SharePoint.SPSite("http://mossdev:8000")
    # loop through webs
    foreach ($web in $site.AllWebs)
    {
      # loop through all lists in web
      foreach ($list in $web.Lists)
      {
        # examine if BaseType of list is NOT a Document Library
        if ($list.BaseType -ne "DocumentLibrary") 
        {
          # forget the rest and return to top
          continue
        }
        # loop through each item
        foreach ($item in $list.Items)
        {
          # get the file, as we need to work with the file as we're in a document library
          $file = $item.File
          # delete all versions
          $file.Versions.DeleteAll()
        }
      }
    }
    $web.Dispose();
    $site.Dispose();
    

    Hope that helps

    - M


    - Michael Mukalian - 2010 MS MVP SharePoint Services - MCTS: MOSS 2007 Configuration - http://www.mukalian.com/blog
    • Marked as answer by David HM Monday, April 11, 2011 1:58 AM
    Monday, April 11, 2011 1:17 AM
  • You can use either Object model code or PowerShell to delete versions,

    $SPweb = Get-SPWeb "http://SharePointSite.com"
    $versionsToKeep =5;
    $SPlist = $SPweb.Lists["Tasks"]
     foreach ($SPitem in $SPlist.Items)
     {
       $currentVersionsCount= $SPItem.Versions.count
     
         if($currentVersionsCount -gt $versionstoKeep)
     {
     for($i=$currentVersionsCount-1; $i -gt $versionstoKeep; $i--)
      {
       $SPItem.versions[$i].delete()
      }
     }
     }
    More info: http://salaudeen.blogspot.co.uk/2011/01/limitcleanup-versioning-in-sharepoint.html
    Tuesday, April 10, 2012 7:36 AM