Forum Discussion
"Trim versions" to be aligned with the library version settings
Inside the "Version history" documentation, it is mentioned about "Trim versions":-
Trim existing versions on site, library, or OneDrive - SharePoint in Microsoft 365 | Microsoft Learn
but seems this is not something aligned with the libraries settings, for example we can have 2 document libraries one with 150 versions limit and one with 80 versions limit.
then we can run this trim script on the whole site or on a specific document library:-
New-SPOSiteFileVersionBatchDeleteJob -Identity $siteUrl -MajorVersionLimit 30 -MajorWithMinorVersionsLimit 30
so we are running one trim script for 2 libraries that have different version limits, but can we dynamically build a trim script which is aligned with each library settings? so it will set the limit = 150 for the first library and 80 for the second library? without having to create a script for each library separably?
1 Reply
- grant_jenkinsIron Contributor
Just wrote a PowerShell script that should get what you want. Note that you should always test any scripts (including this one) in a Test environment first to ensure it works as expected. This script requires special attention to testing since it permanently deletes versions.
It will iterate over all libraries on the site excluding any hidden libraries, and any libraries you add to the exclusion list.
For each library, it will get the current Major and Minor version setting and trim down to that - which mitigates the issue you are currently facing with the overarching SPOSiteFileVersionBatchDeleteJob cmdlet.
This will take in a SiteUrl and Action (either Trim or JobProgress). The SiteUrl will be the site you want to trim libraries on. The Action will either start trimming the libraries or show the progress of the trim job.
Note that you will need to put your current tenant in place of YOURTENANT and your Site Url.
You will need to first switch to the path where the script exists. In my case it is:
cd "C:\Scripts\SPO\Versions"Then run the script using either Trim or JobProgress.
.\Trim-LibraryVersions.ps1 -SiteUrl "https://YOURTENANT.sharepoint.com/sites/YOURSITE" -Action JobProgress OR .\Trim-LibraryVersions.ps1 -SiteUrl "https://YOURTENANT.sharepoint.com/sites/YOURSITE" -Action TrimThe script also has a prompt to ensure you entered the correct SiteUrl before proceeding.
<# SYNOPSIS: This script trims versions on documents libraries across a site to the current version limit on the library. PARAMS - SiteURL - The url of the site you want to trim libraries on - Action - Trim to trim the versions, or JobProgress to check the progress of the trim job #> Param ( [Parameter(Mandatory = $true)] [string] $SiteUrl = $(Throw "Please specify the Site you wish to trim libraries across."), [Parameter(Mandatory = $true)] [ValidateSet("Trim", "JobProgress")] [string]$Action = $(Throw "Please specify either Trim or JobProgress") ) function main() { Connect-SPOService -Url "https://YOURTENANT-admin.sharepoint.com" $site = Get-SPOSite -Identity $SiteUrl #Confirm this is the correct site before continuing Write-Host $site.Title Write-Host $site.Url Write-Host "" Write-Host "Is this site correct?" Write-Host "" $result = Read-Host "Press Y to continue, or any other key to exit and start again" if($result.ToUpper() -ne "Y") { return } #------------------------------------------------------------------ #Lists you want to exclude $excludeLists = "Form Templates", "Style Library", "Site Pages" #Get document libraries that are not hidden and not included in the exlcuded lists $lists = Get-PnPList | where {$_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $false -and $_.Title -notin $excludeLists} #Iterate through each list foreach($list in $lists) { $listTitle = $list.Title Write-Host $listTitle -ForegroundColor Yellow switch ($Action) { "Trim" { #Get the Major and Minor Version Limits for the current list $majorVersionLimit = $list.MajorVersionLimit $minorVersionLimit = $list.MajorWithMinorVersionsLimit Write-Host "Major Versions: $($majorVersionLimit)" Write-Host "Minor Versions: $($minorVersionLimit)" #Only action lists that have versioning enabled if($list.EnableVersioning -eq $true) { New-SPOListFileVersionBatchDeleteJob ` -Site $SiteUrl ` -List $listTitle ` -MajorVersionLimit $majorVersionLimit ` -MajorWithMinorVersionsLimit $minorVersionLimit ` -Confirm:$false } } "JobProgress" { Get-SPOListFileVersionBatchDeleteJobProgress -Site $SiteUrl -List $listTitle } } "" } Disconnect-SPOService } cls main #cd "C:\Scripts\SPO\Versions" #.\Trim-LibraryVersions.ps1 -SiteUrl "https://YOURTENANT.sharepoint.com/sites/YOURSITE" -Action JobProgress #.\Trim-LibraryVersions.ps1 -SiteUrl "https://YOURTENANT.sharepoint.com/sites/YOURSITE" -Action Trim