Share via

Versioning settings not applied

Anonymous
2024-12-04T05:51:28+00:00

I got a notification that my SharePoint usage is full. Upon further investigation, we realized that SharePoint is creating too many versions of documents which is eating up space. I have seen 100MB files have 500 versions eating up around 50GB of space. As a temporary step, deleted the versions for some files manually. Later, I updated the versioning settings to delete all versions older than 30 days and limit the number of versions to 100.

This has been a month ago. However, even today, I have 500 versions for many files as old as 3-4 years. Is there a fix for this?

Microsoft 365 and Office | SharePoint | For business | Other

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

1 answer

Sort by: Most helpful
  1. Anonymous
    2024-12-04T06:59:57+00:00

    Dear Bhargava Grandhi

    Good day! Thank you for posting to Microsoft Community. I am happy to help you

    According to our investigation, you can use the below PowerShell command to achieve your requirement.before running the command, please change <SharePoint/site/URL> to your actual domain name.

    in the command specify the how many versions do you want to keep.

    You first need PowerShell 7 with pnp module installed.

    Please note you need to use PowerShell 7 with PNP module connected to SPO services

    #Config Parameters
    $SiteURL = "https://yourdomain.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
    }
    

    Thank you for your time and patience, we hope you have a great day!

    Sincerely

    Community Moderator | Sophia

    Was this answer helpful?

    0 comments No comments