Plan log retention before removing files
Create a scoped, dated retention plan while preserving every input file.
Plan against a fixed snapshot
Retention rules combine scope, time, and an action. This fictional policy considers only .log files directly inside /home/user/logs whose supplied modification instant is strictly older than 30 days at 2026-10-31T00:00:00Z. A file exactly at the cutoff is retained.
Use file-times.csv for the dated snapshot. The sandbox creates fresh virtual files when an exercise loads, so their current filesystem timestamps are not the historical values under review. Injecting a fixed reference instant also makes the result reproducible tomorrow.
[DateTimeOffset]::Parse('2026-10-31T00:00:00Z').AddDays(-30)
Scope matters as much as age
Check the directory and extension explicitly. An old text document in the log directory and an old log in the reports directory are both outside this rule. The inputs use normalised absolute paths; this is not an exercise in accepting arbitrary paths from an untrusted caller.
One filename contains brackets. Use -LiteralPath when reading its byte length so PowerShell does not interpret those characters as a wildcard pattern.
Emit ReviewRemoval findings with each file's path and actual byte length. Do not delete, move, or rewrite files. A review plan is useful on its own, and it gives a person a concrete result to assess before any later cleanup operation. Applicable retention requirements and holds must be resolved in the real environment.
Go deeper
Import-Csv '/home/user/file-times.csv' | Select-Object Path, ModifiedUtc
Put it into practice.
Use file-times.csv and the fixed reference instant 2026-10-31T00:00:00Z. Select .log files directly in /home/user/logs that are strictly older than 30 days. Emit Path, Bytes (actual file length), and Action ReviewRemoval. Keep the exact-cutoff file and all out-of-scope files. Do not modify or remove any file.
See an explained solution
$cutoff = [DateTimeOffset]::Parse('2026-10-31T00:00:00Z').AddDays(-30)
foreach ($row in Import-Csv '/home/user/file-times.csv') {
if ([IO.Path]::GetDirectoryName($row.Path) -eq '/home/user/logs' -and [IO.Path]::GetExtension($row.Path) -eq '.log' -and [DateTimeOffset]::Parse($row.ModifiedUtc) -lt $cutoff) {
[pscustomobject]@{ Path=$row.Path; Bytes=(Get-Item -LiteralPath $row.Path).Length; Action='ReviewRemoval' }
}
}
A fixed cutoff and explicit directory/extension filters prevent the plan from drifting with the clock or expanding beyond its intended scope. The resulting two findings propose review and leave all files intact.