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.

Delete older than files on Windows 10
Delete older than files on Windows 10
  • To delete files older than 30 days on Windows 10, use the “ForFiles” command.
  • The command is: ForFiles /p “C:\path\to\folder” /s /d -30 /c “cmd /c del /q @file”.
  • Change “30” to the number of days you want and the folder path.

On Windows 10, you can use Command Prompt and Task Scheduler to automatically delete files older than a certain number of days to free up space and keep your files organized.

The Settings app includes Storage Sense, a feature that automatically runs when the storage is low in space. Alongside the ability to delete temporary files, you can also enable the feature to delete files in the recycle bin or Downloads folder that haven’t changed in the last 30 days. However, it is limited and doesn’t offer an option to monitor additional folders to delete files that haven’t changed in the previous 60 days.

When you store temporary files from the internet or projects in different folders, it’s possible to use the ForFiles command in Command Prompt and Task Scheduler to delete files from any folder older than a specific number of days.

In this guide, you will learn the steps to manually delete files that haven’t been modified in a specific number of days and the instructions to create an automated task that will delete files older than a certain number of days inside any folder on Windows 10.

Important: Before using the command on the folder you want to organize, it is recommended to test these steps using a test folder, as using the incorrect parameters and other mistakes can cause the deletion of the wrong files.

Delete files older than X days using ForFiles on Windows 10

If you have different folders with many files and would like to clean up by deleting those 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 on Windows 10, use these steps:

  1. Open Start on Windows 10.

  2. Search for Command Prompt, right-click the result, and select the Run as administrator option.

  3. Type the following command to delete files on Windows 10 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 command, 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.

    Command Prompt ForFiles

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 the ForFiles tool to execute the command (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.

Delete files older than X days automatically using Task Scheduler 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 whenever you want to free up space.

To automate the process, you must use the Task Scheduler on Windows 10 to create a task executing 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:

  1. Open Start.

  2. Search for Task Scheduler and click the top result to open the experience.

  3. Right-click the “Task Scheduler Library” folder.

  4. Click the New Folder option.

  5. Type any name for the folder and click the OK button. (We’re creating a new folder to keep tasks organized and separated from the system tasks.)

  6. Right-click the recently created folder and select the Create Task option.

  7. In the “Name” box, enter a name for the task.

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

    Task Scheduler General tab

  9. Clear the “Do not store password” option.

  10. Click the “Triggers” tab, and click the New button.

  11. Select the “On a schedule” option using the “Begin the task” setting.

  12. Under “Settings,” specify when you want the task to run (for example, On time, Daily, Weekly, or Monthly). Whatever option you select, set the Start settings on the right side.

  13. Click the OK button.

    Task Scheduler Trigger settings

  14. Click the “Actions” tab, and click the New Button.

  15. Use the “Actions” drop-down menu and select the “Start a program” option.

  16. In the “Program/script” box, type the following command:

    ForFiles
  17. 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 command, 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 the last modified date.

    Task Scheduler Action settings

  18. Click the OK button.

  19. 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.
  20. Click the OK button.

    Task Scheduler additional settings

  21. If prompted, enter your administrative username and password.

  22. Click the OK button.

Once you complete the steps, the command will run on the schedule deleting the files 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.

About the author

Mauro Huculak is a Windows How-To Expert who started Pureinfotech in 2010 as an independent online publication. He has also been a Windows Central contributor for nearly a decade. Mauro has over 14 years of experience writing comprehensive guides and creating professional videos about Windows and software, including Android and Linux. Before becoming a technology writer, he was an IT administrator for seven years. In total, Mauro has over 20 years of combined experience in technology. Throughout his career, he achieved different professional certifications from Microsoft (MSCA), Cisco (CCNP), VMware (VCP), and CompTIA (A+ and Network+), and he has been recognized as a Microsoft MVP for many years. You can follow him on X (Twitter), YouTube, LinkedIn and About.me.