How to automatically delete files in Windows

I previously wrote about DropIt, a program that automatically moves or copies files when new files appear in a folder. This can be useful, for example, if you have limited space on your local hard drive and want to move all your downloads to an external drive.

If you want to delete files automatically, there are two ways to do it in Windows. The first method involves downloading a free program called AutoDelete, which allows you to schedule the deletion of files in a specific folder. I’ve already written two detailed guides on using the program (here and here), so check them out if you prefer a free program to get the job done.

The second way to delete files is to create a batch file and then schedule it to run. You can do all this without installing third-party software. In this article, I will walk you through the steps to create a batch file and then use the task scheduler to run the script regularly.

Step 1 – Create a batch file

If creating a batch file sounds a bit scary or too technical, don’t worry, because you don’t need to know what it means. I’ll explain what to copy and paste, where and which settings can be changed. First, open Notepad and 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 okay, as I’ll explain below. It basically tells Windows to delete all files in the C:\Test folder and subfolders that are older than 5 days. This is what your Notepad file should look like.

Before we go into more detail about the command, let’s save the file and try it out. First, create a folder on your computer called Test at the root of drive C. Next, click Filesave and save the file as a batch file. To do this, enter name followed by .bat and then change Save as type drop down list to All files.

save as a batch file

Note that you can save the file anywhere on your hard drive that you like, it doesn’t matter. Now create some dummy files in Test and double-click the folder Delete.bat file to run it. Delete something? Probably not!

The reason nothing has been removed is because the team has /D -5, which means files older than 5 days. To delete any file, regardless of when it was created, you can either change -5 to -0 or you can delete /D -5 part at all. Now if you run it all files will be deleted.

To configure the command, the first thing you can do is change the directory to something other than C:\Test. It’s as simple as copying the path from Windows Explorer to the desired directory and pasting it into a command in Notepad.

copy the path guide

It follows -s option you see after the directory path. This indicates that the command should also browse all subfolders. If you don’t want to remove files from subfolders, remove the -s option.

Then there is – m and *.*, which means that the command should evaluate files of any type. If you only want to delete a specific type of file in a folder, such as PDF files or JPG images, simply change *.* to *.pdf or *.jpeg and only those files will be deleted.

The /D -X the part we already talked about about how old files have to be to be eligible for deletion. You can leave it with a value greater than 1, set it to 0, or remove it altogether. That’s pretty much all we need to know about the team.

There are a few things to note about running this command. First, when files are deleted, they don’t go to the recycle bin, they 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 into a single file. For example, here I create 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 how old the files are.

delete the batch file

Step 2 – Schedule the batch file

Now that your batch file is created and saved, let’s 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 head over to that page to get started. Scroll down to Schedule a batch file at PC startup section and continue.


The only thing you need to change is the trigger. You can choose from Daily, Weekly, Monthly, When the computer starts up, When I log in or When a certain event is registered.

When you choose something like this Weekly or Monthly and press Furtheryou will get a new screen where you can set the exact time and days you want the script to run.

weekly schedule

monthly schedule

Hopefully this is a good solution for most people who need a few simple automated tasks to delete files on their PCs. If you have any questions, feel free to leave a comment. Enjoy!

Leave a Reply

Your email address will not be published.