How to delete files older than X days automatically on Windows 10

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

The Settings app includes Storage sense, a feature that can run automatically when running low on storage to free up space. Alongside the ability to delete temporary files, you can also enable the feature to delete files that have been in the recycle bin or Downloads folder that haven’t changed in the last month. However, the feature is limited and doesn’t offer an option to monitor additional folders to delete its files that haven’t changed in the last 30 days.

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

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

Important: Before running the command on the folder you want to cleanup, make sure to test the command using a test folder, as specifying the wrong parameter can cause to delete the wrong files.

How to use ForFiles to delete files older than X days on Windows 10

If you have different folders with a lot of files and you would like to cleanup by deleting those files that are older than certain days, you can use these steps:

  1. Open Start.

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

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

    ForFiles using Command Prompt

ForFiles command breakdown

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

  1. Open Start.

  2. Search for Task Scheduler and click the result.

  3. Right-click the Task Scheduler Library folder.

  4. Click New Folder.

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

  6. Right-click the recently created folder.

  7. Select Create Task.

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

  9. In the “General” tab, under the “Security options” section, select the Run whether user is logged on or not option. (This is the option that will make the command window not to appear when the task runs automatically.)

    Task Scheduler General tab

  10. Don’t check the Do not store password option.

  11. Go to the “Triggers” tab, and click the New button.

  12. Using the “Begin the task” drop-down menu, select On a schedule.

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

  14. Click OK.

    Task Scheduler Trigger settings

  15. Go to the “Actions” tab, and click the New Button.

  16. Using the “Actions” drop-down menu, select Start a program.

  17. Under “Settings,” type the following command:

    ForFiles
  18. In the “Add arguments” box type the following command and click OK

    /p "C:\path\to\folder" /s /d -30 /c "cmd /c del @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.

    Task Scheduler Action settings

  19. Go to 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 missed.
    • If the task fails, restart everything.
  20. Click OK.

    Task Scheduler additional settings

  21. Enter your administrative username and password.

  22. Click OK.

Once you’ve completed the steps, the command will run on the schedule deleting the files in the location that are older than the number of days you specified. Just remember not to change the name or move the folder to another location, otherwise the task will fail.