SharePoint Online: Delete All Files Older than 30 Days in a Document Library using PowerShell
Requirement: Delete All Files Older than 30 Days in a SharePoint Online Document Library.
PowerShell to Delete Files Older than 30 Days in SharePoint Online
I had to remove all files older than 30 days from a SharePoint Online Online document library. This PowerShell lets you to remove all files created 30 days ago (or more!) from a SharePoint Online document library.
PowerShell to Delete Files Older than 30 Days in SharePoint Online
I had to remove all files older than 30 days from a SharePoint Online Online document library. This PowerShell lets you to remove all files created 30 days ago (or more!) from a SharePoint Online document library.
#Config Variables $SiteURL = "https://crescent.sharepoint.com/sites/ops/" $LibraryName = "Documents" #Connect to PnP Online Connect-PnPOnline -Url $SiteURL -UseWebLogin #Define Query to Filter Files that were 'Created' 30 days ago (or More!) $Query= "<View Scope='RecursiveAll'> <Query> <Where> <And> <Lt> <FieldRef Name='Created' Type='DateTime'/> <Value Type='DateTime' IncludeTimeValue='TRUE'> <Today OffsetDays='-30'/> </Value> </Lt> <Eq> <FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value> </Eq> </And> </Where> </Query> </View>" #Get All Files matching the query $Files = Get-PnPListItem -List $LibraryName -Query $Query -PageSize 500 #Loop through each File Write-host -f Green "Total Number of Files Found:"$Files.Count ForEach($File in $Files) { #Send File to recycle bin Write-Host "Deleting File Created On:" $File.FieldValues.Created -f Yellow Move-PnPListItemToRecycleBin -List $LibraryName -Identity $File.Id -Force Write-Host "`tDeleted File at:" $File.FieldValues.FileRef -f Green }Similarly, you can delete files based on "Modified" date as well!
No comments:
Please Login and comment to get your questions answered!