Forum Discussion

CSHero's avatar
CSHero
Copper Contributor
Sep 29, 2023

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 PowerShell to quickly delete all versions except the last 20?

 

Thank you!

  • LeonPavesic's avatar
    LeonPavesic
    Silver Contributor

    Hi CSHero

    it is possible to limit SharePoint versioning to 20 versions. To do this:

    1. Go to the document library or list where you want to limit versioning.
    2. Click Settings > Library Settings.
    3. Under Versioning Settings, select Create major versions.
    4. In the Keep the following number of major versions box, enter 20.
    5. 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>

     

    • CSHero's avatar
      CSHero
      Copper Contributor

      Hi LeonPavesic

       

      thanks for your quick reply!

      Unfortunately I get the following message when trying to change:

      "You must enter a number between 100 and 50,000."

      • LeonPavesic's avatar
        LeonPavesic
        Silver Contributor

        Hi CSHero,

        I have made some research, the message you are receiving indicates that the minimum number of major versions that you can keep in SharePoint Online is 100. This is a security measure to prevent accidental data loss.

        There are a few workarounds that you can use to limit versioning to 20 versions or less:

        1. Use minor versions. Minor versions are not included in the major version count. So, if you set the major version limit to 100 and the minor version limit to 20, you will be able to keep a total of 120 versions of your files.
        2. Use a third-party versioning tool. There are a number of third-party tools available that can help you to manage versioning in SharePoint Online. These tools typically offer more flexibility than the built-in versioning features in SharePoint Online, such as the ability to set custom versioning limits and to create versioning policies.
        3. Use PowerShell to delete old versions. You can use the PowerShell script that LeonPavesic provided to delete all versions of files and list items except the last 20. However, it is important to note that this script will delete all versions of files and list items, even if they are still needed. Therefore, it is important to make sure that you have a backup of your data before running this script.

        Which workaround you choose will depend on your specific needs and requirements.

        Please click Mark as Best Response & Like if my post helped you to solve your issue.
        This will help others to find the correct solution easily. It also closes the item.


        If the post was useful in other ways, please consider giving it Like.


        Kindest regards,


        Leon Pavesic
        (LinkedIn)

Resources