Cleaning up old Sharepoint Files

Download /Install the PNP module :

  • I have find a very usefull script To archive files in Sharepoint
  • This script is setup to clean up 8 Year Old files but you may modify this on the script
  • The files will go into the recycle bin site . Effectively you have a rollback from disaster
  • I would recommend to setup a Retention policy just in case or check with the legal team before running such scripts, talk to your users & company first!


  1. Fill The Parameters ( your site URL )
  2. Modify the days or date ( How old your files need to be to be deleted) in Days
  3. Run the Script ( with no delete ) and review the output file of your files at c:\temp\ ( Create the folder if necessary )
  4. Once you have Reviewed the files to delete , you can uncomment the “deletion line”

Remember each year is : 365 Days

$SiteURL = "Your Site URL"
$CSVPath = "C:\Temp\FilesDeleted.csv"
$DataCollection = @()
#Connect to SharePoint Online site
Connect-PnPOnline $SiteURL -Interactive
#Get all Documents Libraries from the site
$ExcludedLists  = @("Style Library", "Wiki", "Form Templates","Images","Pages","Site Pages","Preservation Hold Library","Site Assets")
$DocumentLibraries = Get-PnPList | Where {$_.Hidden -eq $False -and $_.ItemCount -gt 0 -and $ExcludedLists -notcontains $_.Title -and $_.BaseType -eq "DocumentLibrary"}
#Loop through all document libraries
ForEach ($List in $DocumentLibraries)
    #Get all Files that are not modified in the past 8 years 
    $global:counter = 0;
    $ListItems = Get-PnPListItem -List $List -PageSize 2000 -Fields Created, Modified -ScriptBlock `
        { Param($items) $global:counter += $items.Count; Write-Progress -PercentComplete ($global:Counter / ($List.ItemCount) * 100) -Activity `
             "Getting Documents from Library '$($List.Title)'" -Status "Getting Files $global:Counter of $($List.ItemCount)";} | ` 

#Modify in this area on (add.days(-Number of days the file las modified )
                 Where {$_.FileSystemObjectType -eq "File" -and $_.FieldValues.Modified -lt (Get-Date).AddDays(-2920)}
    $ItemCounter = 0
    #Iterate through each item
    Foreach ($Item in $ListItems)
        $DataCollection +=New-Object PSObject -Property ([Ordered] @{
            Name  = $Item.FieldValues.FileLeafRef
            RelativeURL = $Item.FieldValues.FileRef
            CreatedOn = $Item.FieldValues.Created
            ModifiedBy =  $Item.FieldValues.Editor.Email
            ModifiedOn = $Item.FieldValues.Modified
        #Delete the File
       # Remove-PnPListItem -List $List -Identity $Item.ID -Recycle -Force
        Write-Progress -PercentComplete ($ItemCounter / ($ListItems.Count) * 100) -Activity "Deleting Files from Library '$($List.Title)' $ItemCounter of $($ListItems.Count)" -Status "Deleting file '$($Item['FileLeafRef'])"       

This Script is written and many thanks for sharing & Special thank you to Salaudeen Rajack from Sharepoint Diary . Please visit !


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.