Delete Version History in SharePoint Document Library

In this post, I would like to show how to delete version history in SharePoint Document Library. For my scenario, I need to do it to reduce the content database size and who cares about file version history 5 years ago.

There are few ways to achieve it. One of the manual way and involving to action for each document library is:

  1. to set the maximum version in document library settings, let say to 2.
  2. Next document you’ve uploaded will delete all version history of all existing files except the latest 2 versions.

The other way you could do is through PowerShell script. Below is the script to delete all version history for all items in Document Library.

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
# get site
$site = new-object Microsoft.SharePoint.SPSite("http://sitename")
# loop through webs
foreach ($web in $site.AllWebs)
{
   write-host $web.url
   # loop through all lists in web
   foreach ($list in $web.Lists)
   {
      # examine if BaseType of list is NOT a Document Library
      if (($list.BaseType -eq "DocumentLibrary") -and ($list.EnableVersioning))
      {
         # Keep latest 2 versions
         $list.MajorVersionLimit = 2
         $list.Update()
         foreach($item in $list.Items)
         {
            $item.SystemUpdate($false)
         }
      }
   }
}
$web.Dispose();
$site.Dispose();

If you like to delete all version history you could replace line 14-20 above to:

         # Delete all version history
         foreach ($item in $list.Items)
         {
            # work with the file object as we're in a document library
            $file = $item.File
            # delete all versions
            $file.Versions.DeleteAll()
         }

NOTE: DO NOT forget to set your PowerShell execution policy to Remotely Signed

Set-ExecutionPolicy RemoteSigned

This could also be done in .NET managed code as well found here.

Another thing that you could do to reduce SharePoint Content Database is by emptying all recycle bin in site collection.

Advertisements

Tagged: , , , ,

3 thoughts on “Delete Version History in SharePoint Document Library

  1. […] Continuing from my previous post to reduce SharePoint Content Database for test environment by deleting version histories for all libraries in SharePoint Site Collection. […]

  2. monica villanueva 08/31/2016 at 1:32 PM Reply

    Accidentally delete a file with the versions of this so I can still recover?

    • tjenho 10/28/2016 at 8:25 AM Reply

      Do you have backup? You can recover through backup

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: