How to delete files older than X days automatically on Windows 10
You can free up space and keep things organized by only deleting files that are older than a certain number of days in any folder — here’s how to do it.
How to use ForFiles to delete files older than X days on Windows 10
If you have different folders with many files and you would like to clean up by deleting those files that are older than a certain number of days, you can use the ForFiles command.
To use the ForFiles command to delete files older than a certain number of days, use these steps:
- Open Start on Windows 10.
- Search for Command Prompt, right-click the result and select the Run as administrator option.
- 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 /q @file"
In the above command remember to change
"C:\path\to\folder"
specifying the path to the folder you want to delete files and change/d -30
to select files with a last modified date.
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 the command, which must be wrapped in double-quotes. The default is “cmd /c del @file”.
- /q — allows deleting folders without requiring confirmation.
If you want to learn more about these switches, use the ForFiles /?
Command.
How to use Task Scheduler to delete files older than X days automatically on Windows 10
The command in the previous instructions allows you to delete files in a folder older than 30 days, but you need to open Command Prompt and execute the command manually every time you want to free up space.
If you want to automate the process, you need to use the Task Scheduler on Windows 10 to create a task that executes the command at specified intervals.
To create a scheduled task with the ForFiles commands to delete files that haven’t changed in some time, use these steps:
- Open Start.
- Search for Task Scheduler and click the top result to open the experience.
- Right-click the Task Scheduler Library folder.
- Click the New Folder option.
- Type any name for the folder and click OK. (We’re creating a new folder to keep tasks organized and separated from the system tasks.)
- Right-click the recently created folder and select the Create Task option.
- In the “Name” box, enter a name for the task.
- In the “General” tab, under the “Security options” section, select the “Run whether user is logged on or not” option. (This option will make the command window not appear when the task runs automatically.)
- Clear the Do not store password option.
- Click the “Triggers” tab, and click the New button.
- Using the “Begin the task” drop-down menu, select the On a schedule option.
- Under “Settings,” specify when you want the task to run (e.g., On time, Daily, Weekly, Monthly). Whatever option you select, make sure to specify the Start settings on the right side.
- Click the OK button.
- Click the “Actions” tab, and click the New Button.
- Using the “Actions” drop-down menu and select the Start a program option.
- In the “Program/script” box, type the following command:
ForFiles
- Type the following command in the “Add arguments” box and click the OK button.
/p "C:\path\to\folder" /s /d -30 /c "cmd /c del /q @file"
In the above command remember to change
"C:\path\to\folder"
specifying the path to the folder that you want to delete files and change/d -30
to select files with a last modified date. - Click the OK button.
- Click the “Settings” tab, and make sure to check the following options:
- Allow task to be run on demand.
- Run task as soon as possible after a scheduled start is missed.
- If the task fails, restart every.
- Click the OK button.
- If prompted, enter your administrative username and password.
- Click the OK button.
Once you complete the steps, the command will run on the schedule deleting the files in the location older than the number of days you specified. Remember not to change the name or move the folder to another location. Otherwise, the task will fail.