Auto delete backup database

Request for auto delete backup database file when some periods. Maybe every 1 week, early file backup will delete.
Thank you

You can do that yourself with pshell script and automation or task schedular. If you dont want to wait.

3 Likes

Umm so @Jesse how would you filter the records? “Delete anything older than 7 days?” using script…

I personally have not written a script to do this but I do know its 100% possible. You have tempted me to research it and provide an example but I have too many other projects going atm so I simply just provided the idea.

Something like this might be a start:

$limit = (Get-Date).AddDays(-15)
$path = "C:\Some\Path"

# Delete files older than the $limit.
Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force

# Delete any empty directories left behind after deleting the old files.
Get-ChildItem -Path $path -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null } | Remove-Item -Force -Recurse

Here is another:

$root  = 'C:\root\folder'
$limit = (Get-Date).AddDays(-15)

Get-ChildItem $root -Recurse | ? {
  -not $_.PSIsContainer -and $_.CreationTime -lt $limit
} | Remove-Item
2 Likes

What would be more interesting would be a staggered deletion script :stuck_out_tongue:

Something like;
Daily for Week
Weekly for Month or two
Monthly for rest of Year
Annual beyond that

So tempted to have ago but I too have so much on ATM.