A Microsoft file hosting and synchronization service.
Dear Amerhauser Emiliano,
Thank you for reaching out regarding the issue with automatically deleting older File versions.
Per searching Online, I found this script might able to achieve that. See below script details.
Important Note: Running PowerShell scripts involves certain risks, including potential security implications. Though I tested it successfully on my end, please be noted it will have some risk to run the script.
Before starting, please make sure you have PowerShell 7 or above version installed.
You can download it from here. Installing PowerShell on Windows - PowerShell | Microsoft Learn
Open PowerShell 7, you then need to install PnP PowerShell module with the command:
Install-Module PnP.PowerShell
Then you can run script below.
#Config Parameters
$SiteURL = "https://nawalawicresoft.sharepoint.com/sites/Microsoft"
$VersionsToKeep = 5
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Get the Context
$Ctx= Get-PnPContext
#Exclude certain libraries
$ExcludedLists = @("Form Templates", "Preservation Hold Library","Site Assets", "Pages", "Site Pages", "Images",
"Site Collection Documents", "Site Collection Images","Style Library")
#Get All document libraries
$DocumentLibraries = Get-PnPList | Where-Object {$_.BaseType -eq "DocumentLibrary" -and $_.Title -notin $ExcludedLists -and $_.Hidden -eq $false}
#Iterate through each document library
ForEach($Library in $DocumentLibraries)
{
Write-host "Processing Document Library:"$Library.Title -f Magenta
#Get All Items from the List - Exclude 'Folder' List Items
$ListItems = Get-PnPListItem -List $Library -PageSize 2000 | Where {$_.FileSystemObjectType -eq "File"}
#Loop through each file
ForEach ($Item in $ListItems)
{
#Get File Versions
$File = $Item.File
$Versions = $File.Versions
$Ctx.Load($File)
$Ctx.Load($Versions)
$Ctx.ExecuteQuery()
Write-host -f Yellow "`tScanning File:"$File.Name
$VersionsCount = $Versions.Count
$VersionsToDelete = $VersionsCount - $VersionsToKeep
If($VersionsToDelete -gt 0)
{
write-host -f Cyan "`t Total Number of Versions of the File:" $VersionsCount
$VersionCounter= 0
#Delete versions
For($i=0; $i -lt $VersionsToDelete; $i++)
{
If($Versions[$VersionCounter].IsCurrentVersion)
{
$VersionCounter++
Write-host -f Magenta "`t`t Retaining Current Major Version:"$Versions[$VersionCounter].VersionLabel
Continue
}
Write-host -f Cyan "`t Deleting Version:" $Versions[$VersionCounter].VersionLabel
$Versions[$VersionCounter].DeleteObject()
}
$Ctx.ExecuteQuery()
Write-Host -f Green "`t Version History is cleaned for the File:"$File.Name
}
}
}
}
Catch {
write-host -f Red "Error Cleaning up Version History!" $_.Exception.Message
}
We are committed to ensuring you have a smooth experience with Microsoft 365 products, and we appreciate your patience and understanding as we work through this challenge.
If there is anything further we can assist you with in the meantime, please do not hesitate to reach out.
Best regards,