Earlier I wrote about the DropIt program, which automatically moves or copies files when new files appear inside a folder. This can be useful, for example, if you have limited local hard drive space and want to move all of your downloads to an external storage device.
If you want to delete files automatically, there are two ways you can do it in Windows. The first method involves downloading the free AutoDelete app that lets you set up a schedule to delete files in a specific folder. I’ve already written two detailed guides for using the program (here and here
The second way to delete files is to create a batch file and then schedule that batch file to run. All of this can be done without installing third-party software. In this article, I’ll walk you through how to create a batch file and then use the task scheduler to have the script run periodically.
Step 1 – Create a batch file
If creating a batch file sounds a little intimidating or too technical, don’t worry, because you don’t need to know what it means. I will explain what you need to copy and paste, where and what parameters you can change. First, open Notepad, copy and paste the following line of text:
forfiles -p “C: Test” -s -m *. * / D -5 / C “cmd / c del @path”
The line above probably doesn’t make sense and that’s perfectly fine as I’ll explain it below. Basically, it tells Windows to delete all files in the C: Test folder and subfolders older than 5 days. This is how your Notepad file should look like.
Before we get into more details about the command, let’s save the file and test it. First, create a folder on your computer called Test at the root of the C drive. Then click File – Save and save the file as a batch file. To do this, enter a name followed by .bat, and then change the Save as type drop-down menu to All Files.
Note that you can save the file anywhere on your hard drive, it doesn’t matter. Now create some dummy files in the Test folder and then double click the Delete.bat file to run it. Is anything being deleted? Probably not!
The reason nothing was deleted is because the command specified / D -5, which means files are older than 5 days. To delete any file no matter when it was created, you can either change -5 to -0, or you can delete the / D -5 part entirely. Now if you run it, all files will be deleted.
To customize the command, the first thing you can do is change the directory to something other than C: Test. It’s as easy as copying the path from Windows Explorer to the desired directory and pasting it into a command in Notepad.
Next comes the -s option that you see after the directory path. This means that the team must also go through all the subfolders. If you don’t want to remove files from subfolders, remove the -s parameter.
Next comes -m, followed by *. *, Which means the command must evaluate files of any type. If you only want to delete certain types of files in the folder, such as PDF files or JPG images, just change *. * To * .pdf or * .jpeg, and only these files will be deleted.
The / D -X part we already talked about in terms of how old the files need to be before they can be deleted. You can leave it with a value greater than 1, set it to 0, or remove it altogether. That’s all we need to know about the team.
There are several things to note when running this command. First, when files are deleted, they do not end up in the trash, but are deleted permanently, so be careful when using it. Second, the command only deletes files, not folders.
Since this is a batch file, you can also add multiple versions of this command to the same file. For example, here I am creating a batch file that will delete all DOCX files older than 180 days, all PDF files older than 60 days, and all TXT files regardless of the age of the files.
Step 2 – Schedule the batch file
Now that your batch file is created and saved, let’s go ahead and schedule it to run again. To do this, we need to open the task scheduler.
Luckily, I’ve already written an article on how to schedule a batch file, so open that page to get started. Scroll down to the “Schedule batch file on PC startup” section and follow the instructions.
The only thing you need to change is the trigger. You can choose from the following options: daily, weekly, monthly, at computer startup, at logon, or when a specific event is registered.
If you select something like “Weekly” or “Monthly” and click “Next”, a new screen will open where you can specify the exact time and days on which the script should run.
Hopefully this is a good solution for most people who need to perform simple automated file deletion tasks on their PCs. If you have any questions, do not hesitate to leave comments. Enjoy!
–