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
- Open your SharePoint Management Shell.
- Copy the code given below and paste it.
- Run the code given below.
- Open your SharePoint site.
- Check that the execution completed successfully or not.
Code
- $SPsite = new-object Microsoft.SharePoint.SPSite("https://gowtham.sharepoint.com/")
- $website=https:
- $web = $SPsite.OpenWeb($website)
- foreach ($list in $web.Lists)
- {
-
- if ($list.BaseType -ne "DocumentLibrary")
- {
- continue
- }
-
- foreach ($item in $list.Items)
- {
-
- $file = $item.File
- # delete all versions in the document library
- $file.Versions.DeleteAll()
- }
- }
- $web.Dispose();
- $site.Dispose();
-
- If you want to delete this one in particular library use this
-
-
- $SPsite = new-object Microsoft.SharePoint.SPSite("https://gowtham.sharepoint.com/")
- $website=https:
- $web = $SPsite.OpenWeb($website)
- $list="Docuemnts"
- $list = $web.Lists[$list]
- if ($list.BaseType -eq "DocumentLibrary")
- {
- foreach ($item in $list.Items)
- {
-
- $file = $item.File
- $file.Versions.DeleteAll()
- }
- }
- $web.Dispose();
- $site.Dispose();
Thanks for reading this blog.