How to Delete Files Older than a Certain Number of Days on Windows 10

Microsoft is introducing options in Windows 10 Redstone 4 that will allow users to automatically clean the downloads folder and Recycle Bin at regular intervals, but for users who want to remove files without manual input from other directories, third-party solutions are the only solution.

Fortunately, the built-in Windows tools come to the rescue and actually allow you to do just that: remove files older than a certain number of days from a specific folder in a second.

Basically, here’s how the whole thing works before jumping right into details. The commands that you can find below help you build a script (a BAT file) that will automatically look for files older than a number of days defined by you and which would then be removed, leaving the directory clean.

What you need to know is that depending on the folder that you want to clean, administrator privileges could be needed, though it most of the cases, standard users should be able to run these commands just fine.

WHO NEEDS IT? The answer to this question is as simple as it could be: anyone who wants to keep folders clean, like the downloads library, can turn to such scripts to automatically delete folder items. On the other hand, system admins and IT pros managing systems in a larger network are the ones that could find this more helpful than others, as it makes cleaning a breeze without the need for third-party software.

WHY WE NEED IT? Keeping files that you don’t need on your system not only that eats up valuable space, but it also slows down loading that specific folder, so the more often you clean the computer, the better the performance. Also, this tool is recommended to all those who want to perform maintenance regularly.

WHEN WE NEED IT? This depends on how often you want to remove older files from your directories. For example, if you want to remove items older than 30 days from the downloads folder, it’s enough to run the script below every month and you should be good to go.

Now that we answered the basic questions, let’s proceed with the essentials. For this task, we’re going to use the ForFiles command tool whose purpose is to specifically manage files from the command line utility. ForFiles support several parameters and I’ll discuss the ones we’ll use below.

For this guide, we’re using the default Windows 10 downloads folder located at C:UsersUsernameDownloads and the 30 days target as settings, but you can change them to whatever you want.

#2. The standard command that we’re going to use here is the following (I used italic to highlight the areas that you need to change according to your settings – the path represents the folder that you want to clean and 30 is the number of days):

/p represents the path of the folder that you want ForFiles to look in for files older than a specific number of days;/s means that ForFiles will apply the removal rule in sub-directories as well, should any be there;/d is the indicator for the date, in our case 30 days/c sends ForFiles the command to execute the taskdel stands for delete@file represents the name of the file and in this command, it applies the command to all files

At first glance, running this particular command is nothing too difficult, though it goes without saying that you need to manually input the path every single time. Fortunately, you can simplify it a little bit.

Navigate to the folder that you want to clean, open it, press SHIFT and then right-click in the folder. Click the option that reads Open PowerShell window here (or Command Prompt if you’re on an older version of Windows) and enter the following command:

ForFiles /s /d -30 /c “cmd /c del @file”

In this example, we removed the /p (path) parameter because we no longer need it since we’re already in the folder that we need to clean.

There are several other ways to automate the cleaning process of each folder, and you can do it with a .BAT file that includes the following lines:

@echo offForFiles /s /d -30 /c “cmd /c del @file”end

With this script, all files older than 30 days (including the script itself) will be removed when simply launching the BAT file.