Advertisement

How To Delete Files Older Than X Days in Windows 10

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:File Explorer Search Ribbon 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

  1. Open the Search tools tab in the Ribbon (F3).
  2. Click on the Date modified button. It has a drop down list with options.File Explorer Modified Date Drop Down Search
  3. 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.File Explorer Delete Files

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

File Explorer Find Files Older Than

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.

File Explorer Find Files By Date Range

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.

Forfiles Help

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.

  1. Open a new command prompt instance.
  2. 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.File Explorer Delete Files Older Than Cmd

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.File Explorer Delete Downloads Older Than Cmd

Delete Files Older Than X Days Automatically

You can automate this task using the built-in Task Scheduler app.

  1. Open Administrative tools and click on the Task Scheduler icon.
  2. In the left pane, click the item "Task Scheduler Library":Windows 10 Task Scheduler Library
  3. In the right pane, click on the link "Create task":Windows 10 Create Task link
  4. 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".File Explorer Delete Old Files Task
  5. Switch to the "Actions" tab. There, click the "New..." button:
    Windows 10 Create Task window Actions tabWindows 10 Create Task window Actions tab new button
  6. 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"
    Delete Old Files Ts ActionChange the folder path and the number of days to what you need.
  7. Go to the Triggers tab in your task. There, click on the New button.New Trigger Button
  8. 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.Delete Old Files Ts Trigger
  9. 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.Delete Old Files Ts Settings
  10. 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

  1. Open a new PowerShell window.
  2. 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

Author: Sergey Tkachenko

Sergey Tkachenko is a software developer who started Winaero back in 2011. On this blog, Sergey is writing about everything connected to Microsoft, Windows and popular software. Follow him on Telegram, Twitter, and YouTube.

14 thoughts on “How To Delete Files Older Than X Days in Windows 10”

  1. 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.

  2. 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.

    1. Try /q after @files and seems to fix this issue

      ForFiles /p “C:\My Folder” /s /d -30 /c “cmd /c del @file /q”

  3. 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 ].

  4. 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.

    1. 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

  5. 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.

  6. Excellent, Very nice work, Great help.

    Can you also share the command to do the same list or delete folders older then X days.

  7. 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.

  8. 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.

Leave a Reply

Your email address will not be published.

css.php
Using Telegram? Subscribe to the blog channel!
Hello. Add your message here.