Delete files older than N days with forfiles

Windows' forgotten find -mtime equivalent — selects files by age and runs a command on each one.

WindowscleanupFilescmdHigh risk
Run forfiles /P <dir> /S /M *.log /D -30 /C "cmd /c del @path" to delete .log files older than 30 days (test with echo first).

01Command

Command
$ forfiles /P "C:\Logs" /S /M *.log /D -30 /C "cmd /c echo @path"

02What it does

/D -30 matches files last modified more than 30 days ago; /D -2026-09-01 matches a specific date, and /D +7 means newer than 7 days. /S recurses, /M filters by mask, and @path, @fname, @fdate, @ftime are substituted into the /C command. Forfiles is the standard scheduled-task ingredient for log retention on servers.

03Risk

RiskHigh risk
RequirementsWindows Vista or later
!Warning: Replacing echo with del permanently deletes every matching file — always run the command with echo @path first to review exactly what would be deleted.

04Undo

✓No undo needed — this command does not change persistent state.

05Sources

  • FORFILES - Windows CMD
    SS64 · technical · 2026-09-17
    Evidence: SS64 documents /D -30 as files last modified more than 30 days ago, and shows "cmd /c del @path" as the standard deletion pattern.
  • forfiles | Microsoft Learn
    Microsoft · vendor-doc · 2026-09-17
    Evidence: Microsoft documents forfiles /D +/-n to select files with last-modified date older/newer than n days, /S for recursion, and /C to run a command per file with @path variables.

Last verified: 2026-09-17 · Category: Files & Folders

06Related solutions