Windows 10 has a number of improvements that allow you to keep your PC clean. Its recent versions include built-in tools to automatically empty the Recycle Bin and clean the Downloads folder periodically. Unfortunately, if you are running an early build of Windows 10 or a previous version of the OS, these features are not available for you. Here is how you can delete files older than certain days using three different methods.
Advertisеment
To achieve this goal, you don't even need any third-party tools. This can be done using either File Explorer, PowerShell or a batch file.
File Explorer is the default file manager app in Windows 10. It has a special search box. When it gets focused, it shows a number of advanced options in the Ribbon. To activate the search feature in File Explorer, click on the search box or press F3 on the keyboard. The Ribbon will then look as follows: Here is how to use it to delete files older than a certain number of days.
Delete Files Older Than X Days with File Explorer
- Open the Search tools tab in the Ribbon (F3).
- Click on the Date modified button. It has a drop down list with options.
- Select the desired option, like Last week.
File Explorer will filter the results immediately. Select the files you don't need, and press the Delete key to delete files. Alternatively, you can right-click the selection and choose Delete from the context menu.
Tip: You can use your own, custom size filters. All you need is to type the desired filter condition in the search box of File Explorer as follows:
datemodified:11/1/2017 .. 11/20/2017
Instead of 'datemodified', you could use 'datecreated' to find files in a specific date range.
Alternatively, you can type the date related parameter and enter a colon character (:). This will make File Explorer show the date picker. Pick a date or condition from the calendar pop-up. You can click on a date and drag to specify a date range. This way you can filter the results to get exactly what you want.
This method is good for deleting files manually once in a while. If you need to automate the procedure, e.g. clean up the Downloads folder on a periodic basis, you should use either the command prompt or PowerShell methods. Let's review them.
Delete Files Older Than X Days with a Batch File
In my previous article, Find Large Files in Windows 10 Without Third-Party Tools, we have learned about the useful ForFiles console command. This command selects a file (or a set of files) and executes a command on that file.
The switches we can use are as follows:
/S - This switch makes forfiles recurse subdirectories. Like "DIR /S".
/D - Select files with a last modified date. For example,-365 means over a year ago, -30 means a month ago.
/P - To indicate the path to start the search.
/C "command" - This command specifies the command to execute on each file that will be found. Command strings should be wrapped in double quotes.
The default command is "cmd /c echo @file".
The following variables can be used in the command string:
@file - returns the name of the file.
@fname - returns the file name without extension.
@ext - returns only the extension of the file.
@path - returns the full path of the file.
@relpath - returns the relative path of the file.
@isdir - returns "TRUE" if a file type is
a directory, and "FALSE" for files.
@fsize - returns the size of the file in bytes.
@fdate - returns the last modified date of the file.
@ftime - returns the last modified time of the file.
To delete files older that X days, do the following.
- Open a new command prompt instance.
- Type the following command:
ForFiles /p "C:\My Folder" /s /d -30 /c "cmd /c del @file"
Substitute the folder path and the amount of days with desired values and you are done.
For example, to remove the files older than a month from the Downloads folder, use the following command:
ForFiles /p "%userprofile%\Downloads" /s /d -30 /c "cmd /c del @file"
This trick works in all modern versions of Windows including Windows 7, Windows 8, Windows 8.1 and Windows 10.
Delete Files Older Than X Days Automatically
You can automate this task using the built-in Task Scheduler app.
- Open Administrative tools and click on the Task Scheduler icon.
- In the left pane, click the item "Task Scheduler Library":
- In the right pane, click on the link "Create task":
- A new window titled "Create Task" will be opened. On the "General" tab, specify the name of the task. Pick an easily recognizable name like "Delete Old Files".
- Switch to the "Actions" tab. There, click the "New..." button:
- The "New Action" window will be opened. There, you need to specify the following data.
Action: Start a program
Program/script:ForFiles.exe
Add arguments(optional):/p "%userprofile%\Downloads" /s /d -30 /c "cmd /c del @file"
Change the folder path and the number of days to what you need. - Go to the Triggers tab in your task. There, click on the New button.
- Under Begin the task, select "On a schedule" in the drop down list and click the OK button. Specify when you want the task to run.
- Switch to the "Settings" tab. Enable the options
- Allow task to be run on demand.
- Run task as soon as possible after a scheduled start missed. - Click OK to create your task.
That's it.
Finally, if your prefer PowerShell, you can use a special cmdlet to remove old files.
Delete Files Older Than X Days with PowerShell
- Open a new PowerShell window.
- Type the following command:
Get-ChildItem "%userprofile%\Downloads" -Recurse | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-30))}| Remove-Item
If the Get-ChildItem cmdlet finds any files that are older than a month, then the Remove-Item cmdlet will be called for each file to remove it.
That's it.
Support us
Winaero greatly relies on your support. You can help the site keep bringing you interesting and useful content and software by using these options:
If you like this article, please share it using the buttons below. It won't take a lot from you, but it will help us grow. Thanks for your support!
Advertisеment
If you’re willing to go third-party, Delage32 is free, is easier to use than FORFILES, and is much, much faster:
http://www.horstmuc.de/wbat32.htm
It’s been my go-to method for purging old log files and such on servers for 10+ years.
The solution using forfiles.exe seems to produces interactive requests for confirmation on some objects. Thus it is not really suitable for use in task scheduler.
Try /q after @files and seems to fix this issue
ForFiles /p “C:\My Folder” /s /d -30 /c “cmd /c del @file /q”
In the ForFiles examples, @file return only the file name part of the full path name for the file – WHICH MAY CAUSE CATASTROPHIC DELETES OF WRONG FILE IN CURRENT DIRECTORY or just fails to delete – please change @file to @path for full pathname of file to delete [ tested in Windows 10 1803 ].
Hi, we made this script but forfiles don’t work, any ideas?
NET USE \\coresrv2019\\Software /u:coresrv2019\username *pasword*
ForFiles /p “\\coresrv2019\Software\BackupBBDDERP” /s /d -04 /c “cmd /c del @path”
robocopy “E:\Backups\erp” “\\coresrv2019\Software\BackupBBDDERP” /e /maxage:3
NET USE \\coresrv\Software /D
We use @path and @file but same result.
forefiles does not accept UNC paths. You can do something like:
NET USE Z: \\myservername\myshare /u:domain\username *password*
ForFiles /p “z:\someFolderInYourShare” /s /d -04 /c “cmd /c del @path”
NET USE z: /D
How to delete files older than X days including much older?
Great! very useful!
Thanks a lot!
Article is not complete, really need to know the following:
(1) how the command so it shows me what it’s doing first (instead of running del), I doubt my first run will be the only time I want to test this.
(2) Deleting large files is not useful, we want to delete specific types of files more likely.
(3) How do I do this for sub directories etc.
Excellent, exactly what I needed to know
Excellent, Very nice work, Great help.
Can you also share the command to do the same list or delete folders older then X days.
I have tried this command ForFiles /p “My Folder” /s /d -180 /c “cmd /c del @file”
to remove the files having modified date more than 180 days. But it is not able to delete the file of subfolders. Can you please give command to delete the files for subfolders as well.
This is great and I am now removing files. I wish I could also delete sub folders older too! I haven’t found solution yet.
For the powershell version of this, $env:USERPROFILE should be used instead of %userprofile%