How to use ForFiles to delete files older than X days on Windows 10

If you have different folders with a lot of files and you would like to cleanup by deleting those files that are older than certain days, you can use these steps:

  1. Open Command Prompt with Admin rights
  2. Type the following command to delete files that haven’t been modified in the last 30 days and press Enter:
    ForFiles /p "C:\path\to\folder" /s /d -30 /c "cmd /c del @file"

ForFiles command breakdown

  • /p — indicates the pathname to start searching.
  • /s — instructs ForFiles to search inside subdirectories.
  • /d — specifies the last modified date for a file.
  • /c — instructs ForFiles to execute command, which needs to be wrapped in double-quotes, the default is “cmd /c del @file”.

If you want to learn more about these switches, use the ForFiles /? Command.