How To Automatically Delete Files Older Than X Days On Windows 10
Windows 10 has a feature called Storage Sense that frees up space on your system. It targets the Recycle bin and temporary files. It deletes files that are older than 30 days if they have not been used/accessed. The feature is useful but it doesn’t let you add additional folders to it. If you want to automatically delete files older than X days, where X is any number of days you choose at your own discretion, then you need to create a scheduled task and use a command prompt command.
We should warn you that the command used to automatically delete files older than X days doesn’t stop to check if you accessed the file recently. It will look at the ‘Last Modified’ date so if the file was never modified then it will be deleted.
Delete Files Command
The command used to delete files is as follows;
ForFiles /p "C:\path to folder" /s /d -30 /c "cmd /c del @file"
You will have to modify it for the folder you want to delete files from. Make sure that you run this command on a folder. Do not point to system folders such as the Downloads folder or the Desktop folder. You can create a folder in either of these locations and delete files from it but don’t target the folders themselves. The command will look at sub-folders in the folder you point it to.
To modify the command you need to add the correct path to the folder you want to delete files from, and the expiry date you want to set for the files. This is what the modified command looks like;
ForFiles /p "C:\Users\fatiw\Desktop\Screenshots" /s /d -10 /c "cmd /c del @file"
It will delete files that are more than 10 days old, and that reside in the location given after /p. You can test it out with a dummy folder and some old files by running the command in Command Prompt.
Scheduled Task
Open the task scheduler. Click Create Task on the right. Give the task a name so you can find it easily later when/if you decide to delete it. Next, go to the Triggers tab. Click New, and from the New Trigger window, select how often you want the task to run.
Click OK, and then go to the Actions tab. Again, click New at the bottom. In the Action field, select the ‘Start a program’ option. Next, in the Program/Script field, enter “ForFiles”. Finally, in the Add arguments field, enter the command from the previous section but omit the ForFiles part. It should look like this when you enter it;
/p "C:\Users\fatiw\Desktop\Screenshots" /s /d -10 /c "cmd /c del @file"
For the final step, go to the Settings tab and enable the “Allow task to be run on demand” and “Run task as soon as possible after a scheduled start missed”. That’s all you need to do.
Thank you for this blog.
I just did this on a Win2012 R2 Server and had to add the /q parameter for del at a scheduled task:
ForFiles /p “C:\path to folder” /s /d -30 /c “cmd /c del /q @file”
Without this the task scheduler can not complete deleting the file, because it needs a confirmation if you really want do delete it…
And I put the path into the “start in” field, so I only use
/s /d -30 /c “cmd /c del /q @file”
as arguments.