Home » How To » How To Use Task Scheduler To Delete Files Older Than X Days

How To Use Task Scheduler To Delete Files Older Than X Days

You can use the Windows task scheduler to automatically delete files older than a number of days. Here’s how to do it in simple steps.

To delete a file or files in Windows, all you have to do is select them and press the delete button on your keyboard. For special folders like the downloads folder, recycle bin, cache folders, temp folders, etc., Windows has a built-in tool called Storage Sense that can automatically delete files after a certain number of days. For example, if you want to delete files after 30 days, 60 days, or 90 days, you can configure Storage Sense to do exactly that. The downside is that the Storage Sense tool doesn’t support custom folders.

So, to automatically delete files older than X number of days, you have to use the task scheduler. Since the task scheduler lets you execute scripts at a specific time, all we have to do is create a custom script, add it to the task scheduler and then execute it as and when needed. The good thing is, we are going to use a simple PowerShell script to delete files older than a number of days. The script/command itself is pretty easy to understand and configure.

Without much delay, let me show you the steps to use task scheduler to delete files older than a number of days in Windows 10.

Use task scheduler to delete files older than X days

  1. Open task scheduler.
  2. Click on “Create Basic Task.”
  3. Name the new task.
  4. Click “Next”.
  5. Select a trigger.
  6. Configure selected trigger.
  7. Select “Start a program.”
  8. Type “powershell” in the Program/Script field.
  9. Paste the below command in the “Add arguments” field. Replace FOLDER_PATH & NUMBER_OF_DAYS with the actual folder path and number of days.
    -Command "Get-ChildItem -Path 'FOLDER_PATH' -File -Recurse -Force | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-NUMBER_OF_DAYS))}| Remove-Item -Force"
  10. Click “Next”.
  11. Click “Finish”.
  12. Close the task scheduler window.

First, open the Task Scheduler in Windows. You can do that by searching for it in the Start menu or by executing the “taskschd.msc” run command.

open task scheduler

Once the task scheduler has been opened, click on the “Create Basic Task” option on the sidebar to create a new scheduled task.

create basic task

Name the new task anything you want and click “Next.” Just make sure the name is descriptive enough. For example, if you want to delete files older than 30 days, name the task as such.

name the task

Now, select when you want the task to start and click “Next.” In my case, I want the task to start every day. So, I select the “Daily” option. If you want something else, choose accordingly.

select a trigger

Configure the trigger you selected and click “Next”. Depending on the trigger you chose in the earlier step, this screen might be a bit different. For instance, since I chose “Daily”, I need to set the actual time, start date, and how often the task should recur.

configure selected trigger

Since we want to run a PowerShell command, select the “Start a program” option. Click “Next” to continue.

select start a program option

We further need to configure the “Start a program” action. So, fill in the “Program/Script” and “Add arguments” as shown below.

  • Program/script: powershell
  • Add arguments: -Command "Get-ChildItem -Path 'FOLDER_PATH' -File -Recurse -Force | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-NUMBER_OF_DAYS))}| Remove-Item -Force"

Replace FOLDER_PATH with the actual folder path where the files you want to delete are located. Also, replace NUMBER_OF_DAYS with the actual number of days. For example, if you want the command to delete files older than 30 days then replace NUMBER_OF_DAYS with 30. This is how the modified command looks like:

-Command "Get-ChildItem -Path 'D:\WindowsLoop\MyFolder' -File -Recurse -Force | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-30))}| Remove-Item -Force"

use task scheduler to delete files older than number of days

Review the summary window and click on the “Finish” button to complete the task creation process.

finish task creation

Finally, we can test the task to see if it is working as intended. To do that, find the task in the Task Scheduler window, right-click on it and select the “Run” option. You will see a brief flash of the PowerShell command window and the files should be deleted.

run task to delete files older than x days

Important note: All files are deleted permanently. i.e, they are not moved to the recycle bin. If the files are in use or locked by some other program, the PowerShell command might fail to delete one or more files. It might even get angry and throw errors. So, make sure the files will not be in use when the task is executed.

I hope that helps. If you are stuck or need some help, comment below and I will try to help as much as possible.

More things you can do with task scheduler:

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top