Forum Discussion

grahamjones26's avatar
grahamjones26
Copper Contributor
Nov 14, 2023

Script to delete all but last five versions of files SharePoint online

Hi

Would anyone know of a script to delete old versions of files in Sharepoint online document library but keep the last five?

Thanks for reading

  • LeonPavesic's avatar
    LeonPavesic
    Silver Contributor

    Hi grahamjones26,

    you can try to use this script below:

    # This script deletes all but the last 5 versions of each document in a SharePoint Online library
    
    # Connect to SharePoint Online site
    $siteUrl = "https://contoso.sharepoint.com/sites/MySite"
    Connect-PnPOnline -Url $siteUrl -Interactive
    
    # Get the library name
    $libraryName = "Documents"
    
    # Get all the items in the library
    $items = Get-PnPListItem -List $libraryName
    
    # Loop through each item
    foreach ($item in $items) {
        # Get the file object
        $file = $item.File
    
        # Get the versions of the file
        $versions = $file.Versions
    
        # Sort the versions by creation date in descending order
        $sortedVersions = $versions | Sort-Object -Property Created -Descending
    
        # Skip the first 5 versions and delete the rest
        $sortedVersions | Select-Object -Skip 5 | ForEach-Object {
            $_.DeleteObject()
        }
    }


    You need to adapt it to your tenant and to your needs.

    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)

    • grahamjones26's avatar
      grahamjones26
      Copper Contributor

      LeonPavesic 

      Thank you for your kind help.

      I have ran the script and receive the following error. What have I done wrong?

       

      An error occurred while enumerating through a collection: The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be
      explicitly requested..
      At line:22 char:5
      + $sortedVersions = $versions | Sort-Object -Property Created -Desc ...
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : InvalidOperation: (Microsoft.Share...nt.FileVersion]:<GetEnumerator>d__3) [], RuntimeException
      + FullyQualifiedErrorId : BadEnumeration

      An error occurred while enumerating through a collection: The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be
      explicitly requested..
      At line:22 char:5
      + $sortedVersions = $versions | Sort-Object -Property Created -Desc ...
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : InvalidOperation: (Microsoft.Share...nt.FileVersion]:<GetEnumerator>d__3) [], RuntimeException
      + FullyQualifiedErrorId : BadEnumeration

Resources