Forum Discussion
CSHero
Sep 29, 2023Copper Contributor
Sharepoint versions
Hi, unfortunately, the file versions take up all our space in Sharepoint. Is it possible to limit the versioning to 20 versions? So that one keeps only 20 versions? Also, is there a way to use ...
LeonPavesic
Sep 29, 2023Silver Contributor
Hi CSHero
it is possible to limit SharePoint versioning to 20 versions. To do this:
- Go to the document library or list where you want to limit versioning.
- Click Settings > Library Settings.
- Under Versioning Settings, select Create major versions.
- In the Keep the following number of major versions box, enter 20.
- Click OK.
Changing the versioning settings will not affect any existing versions of files or list items.
To use PowerShell to quickly delete all versions except the last 20, you can use the following script:
# Get the list of all document libraries in the current site
$libraries = Get-PnPWeb -Url "https://contoso.sharepoint.com/sites/mysite" -ListInfo
# Iterate through each library and delete all versions except the last 20
foreach ($library in $libraries) {
# Get the list of all files in the library
$files = Get-PnPListItem -List $library.Title
# Iterate through each file and delete all versions except the last 20
foreach ($file in $files) {
# Get the list of all versions of the file
$versions = Get-PnPListItemVersion -List $library.Title -ItemId $file.Id
# Delete all versions except the last 20
for ($i = 0; $i -lt $versions.Count - 20; $i++) {
Remove-PnPListItemVersion -List $library.Title -ItemId $file.Id -VersionNumber $versions[$i].VersionNumber
}
}
}<div class=""> <p>To use this script, you will need to have the PnP PowerShell module installed. You can install it from the PowerShell Gallery:</p><div class=""><div class=""><li-code lang="applescript">Install-Module -Name PnP.PowerShell<p>Once you have installed the PnP PowerShell module, you can run the script by copying and pasting it into a PowerShell console.<br /><br /></p><p><strong>Please note:** This script will delete all versions of files and list items except the last 20. It is important to make sure that you have a backup of your data before running this script.<br /><br /></strong></p><div><p>Please click <strong>Mark as Best Response & <strong>Like if my post helped you to solve your issue.<br />This will help others to find the correct solution easily. It also closes the item.<br /></strong></strong></p><p>If the post was useful in other ways, please consider giving it <strong>Like.<br /></strong></p><p>Kindest regards,<br /></p><p>Leon Pavesic<br />(<a href="https://www.linkedin.com/in/leon-pavesic/" target="_blank">LinkedIn)</a></p></div>