In a SharePoint document library, we can see the document versions across the site collections. We have a large amount of document versions, some of which will not be required.

The code snippet given below will execute and clean up all the document version's history across the site collections.

Steps
  1. Open your SharePoint Management Shell.
  2. Copy the code given below and paste it.
  3. Run the code given below.
  4. Open your SharePoint site.
  5. Check that the execution completed successfully or not.
Code
  1. $SPsite = new-object Microsoft.SharePoint.SPSite("https://gowtham.sharepoint.com/")
  2. $website=https://gowtham.sharepoint.com/tutorials/tutorials
  3. $web = $SPsite.OpenWeb($website)
  4. foreach ($list in $web.Lists)
  5. {
  6. if ($list.BaseType -ne "DocumentLibrary")
  7. {
  8. continue
  9. }
  10. foreach ($item in $list.Items)
  11. {
  12. $file = $item.File
  13. # delete all versions in the document library
  14. $file.Versions.DeleteAll()
  15. }
  16. }
  17. $web.Dispose();
  18. $site.Dispose();
  19. If you want to delete this one in particular library use this
  20. $SPsite = new-object Microsoft.SharePoint.SPSite("https://gowtham.sharepoint.com/")
  21. $website=https://gowtham.sharepoint.com/tutorials/tutorials
  22. $web = $SPsite.OpenWeb($website)
  23. $list="Docuemnts"
  24. $list = $web.Lists[$list]
  25. if ($list.BaseType -eq "DocumentLibrary")
  26. {
  27. foreach ($item in $list.Items)
  28. {
  29. $file = $item.File
  30. $file.Versions.DeleteAll()
  31. }
  32. }
  33. $web.Dispose();
  34. $site.Dispose();
Thanks for reading this blog.