Share via

Automatically delete older File versions.

Anonymous
2024-09-12T06:56:12+00:00

Dear community,

i have searched the internet trying to find a solution for the troubles one of our users is having, but without success. They have a File of over 50 GB save in One Drive, which they interact daily with. Naturally after changing/editing the file, an older version is created, consuming space in One Drive.

Unfortunately it has come to the point, where his capacity is alway full and he would like to have an automated way to deleted older file versions .

It doesnt help that Microsoft doesnt allow you to set less than 100 main versions of a file.

Is there a way to do that? Can i build in power shell script or power app that deletes versions older than 7 days?

If there is a guide or script somewhere i would appreciate the help a ton

Best regards

Emiliano

Microsoft 365 and Office | OneDrive | For business | Windows

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

Answer accepted by question author

  1. Anonymous
    2024-09-13T14:37:40+00:00

    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,

    1 person found this answer helpful.
    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Anonymous
    2024-09-19T05:39:08+00:00

    Dear Amerhauser Emiliano,

    Yes, you can use this script to delete versions for a specific file.

    #Define Parameters
    $SiteURL = "https://crescent.sharepoint.com/sites/Retail"
    $FileRelativePath = "/sites/Retail/Shared Documents/Sitemaps.xlsx"
    $VersionsToKeep = 5
     
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive
     
    #Get File Versions
    $File = Get-PnPFile -Url $FileRelativePath
    $Versions = Get-PnPProperty -ClientObject $File -Property versions
     
    #Notification of file collected
    Write-host -f Yellow "Scanning File:"$File.Name
    $VersionsCount = $Versions.Count
    write-host -f Cyan "`t Total Number of Versions of the File:" $VersionsCount
     
    $VersionsToDelete = $VersionsCount - $VersionsToKeep
    If($VersionsToDelete -gt 0)
    {
        write-host -f Cyan "`t Total Number of Versions to be deleted:" $VersionsToDelete
        #Delete versions
        For($i=0; $i -lt $VersionsToDelete; $i++)
        {
            If($Versions[$i].IsCurrentVersion)
            {
                $VersionsToDelete++
                Continue
            }
            write-host -f Cyan "`t Deleting Version:" $Versions[$i].VersionLabel
            Remove-PnPFileVersion -Url $FileRelativePath -Identity $Versions[$i].ID -Force
         }
         Write-Host -f Green "`t Version History is cleaned for the File:"$File.Name
    }
    
    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2024-09-23T12:14:41+00:00

    Thanks Sophia,

    just what i needed.

    Have a nice day

    Emiliano

    0 comments No comments
  3. Anonymous
    2024-09-16T06:55:09+00:00

    Hello Sophia,

    thanks for taking the time in checking that.

    Can i modify this script so that it only affects only the "issue file", lets say: MEGAFILE.zip located in Documents?

    What should i change from the script you sent

    Best regards and thanks again

    Emiliano

    0 comments No comments
  4. Anonymous
    2024-09-13T14:33:02+00:00
    0 comments No comments