How to Automatically Delete Files Older than a Specific Number of Days on Windows PC

You have a folder where your log files get accumulated and the folder is growing bigger and bigger with each passing day. Now for the new log files to come in, you sure need some space. But if you have to do it manually, that’s definitely going to take some time. How about you double click on a batch file and the batch file would delete files older than a specified number of days? Or let’s say how about the batch file delete all the files of a particular extension, say .temp, whenever you double click on it? Sounds great? This article is so custom made for you then.

Read on, to learn how you can easily delete all the files, or specific files, older than a specific number of days with the help of a simple batch script.

Sample Scenario

Please make sure that you have all the files to be considered for deletion under a parent folder. It’s ok for this parent folder to have subfolders, as the batch script will look for files under subfolders as well, to delete them. In the following example, I have a folder named File Repo, under which I have all the files that I need to consider for deletion.

 

1 Sample Min

Section 1: How to Automatically Delete All Files Older than a Specific Number of Days

Step 1: Go to any location of your preference, it can even be your Desktop. This is where your batch file will be created.

Now right click anywhere on an empty space and then click on New and then on Text Document.



 

2 New File Min

 

Step 2: Click on the newly created file once and then hit the F2 key to rename it.

Give the file any name of your choice, but please make sure to give the extension of the file as bat. In the example below we have named our batch file as geekPageBatchDelete.bat.

Note: It is very important to give the bat extension.

 

3 Name File Min

 

Step 3: Hit the Enter key and you will get the following Rename window, where you have to hit the Yes button to proceed.

 

4 Rename Min

 

Step 4: As next, right click on the newly created batch file and then click on the Edit option.

 

5 Edit Min

 

Step 5: Copy and paste the following code onto the Notepad file that opens before you.

@echo off
forfiles /p "<path_to_the_folder>" /s /m *.* /D -<number_of_days> /C "cmd /c del @path"

Important Note: Please replace <path_to_the_folder> with the complete path of your parent folder that has your files. Also, replace <number_of_days> with the actual number of days. All the files that are older than the given number of days will be deleted. You can refer to the screenshot below for a clearer picture.

 

6 Notepad Image Min

Script Explanation

This section is for our geek readers who are curious about the inside working of our small scripts. Let’s see what’s happening in the script above.

forfiles – This keyword is used to execute the command on every file inside the selected folder.



/p <path_to_folder>/p sets the path to the string, which is <path_to_folder>, that follows it.

/s – This parameter makes sure that all the files inside the subfolders are also traversed.

/m *.*/m parameter is to make the matching. The wildcard *.* means all the files are to be considered, regardless of their names and extensions.

/D – Using this parameter you get to specify a date.

/C – It is after this parameter that we specify the command that needs to be executed on all the files that meet the conditions.

del @path – This will delete the file at the given path, that meets the rest of the conditions above. Since we have used forfiles, all the files will be deleted one by one.

 

Step 6: Now hit the File tab at the top and then hit the Save option.

 

7 Save File Min

 

ALERT: Before going ahead with Step 7, make sure you have a backup if it is required. Once you execute the batch file, all the files in the specified folder that are older than the specified number of days will be deleted. 

Step 7: Now, simply double click on the batch file to execute it, and to see the magic.

 

8 Execute Batch Min

 

Section 2: How to Automatically Delete a Specific File Older than a Specific Number of Days

Now let’s say you have a specific file that you need to be deleted if it is older than a specific number of days. In this case, you can follow the exact same steps as in the section above, but in Step 5, please copy and paste the following code instead.

@echo off
forfiles /p "<path_to_the_folder>" /s /m <actual_file_name> /D -<number_of_days> /C "cmd /c del @path"

Replace the following:

<path_to_the_folder> –> The actual path to your folder that contains the file to be deleted.

<actual_file_name> – The name of the file that is to be deleted.

<number_of_days> – The actual number of days.

 

9 Specific File Min

 

Section 3: How to Automatically Delete Files with a Specific File Extension and Older than a Specific Number of Days

Now let’s say you want to delete all the txt files in a folder, or the docx files, that are older than a specific number of days. In that case, you can follow the steps mentioned in Section 1. But just like in Section 2, in Step 5, copy and paste the following code instead.

@echo off
forfiles /p "<path_to_the_folder>" /s /m *.<extension> /D -<number_of_days> /C "cmd /c del @path"

Replace the following:



<path_to_the_folder> – This is the path of the folder containing the file to be deleted.

<extension> – The extension type that you want to be deleted.

<number_of_days> – If the file is older than the number of days specified and is of the extension mentioned using <extension>, it will be deleted.

 

10 Extension Min

Section 4: How to Automatically Delete All Files

Let’s see one more case where you want to delete all the files inside a specific folder irrespective of their extension and the number of days since they have been created. In that case, you need to specify the number of days as zero. The steps are all same as mentioned in Section 1, the only difference is in the code that you paste on Step 5, which should be as below.

@echo off
forfiles /p "<path_to_the_folder>" /s /m *.* /D -0 /C "cmd /c del @path"

Replace the following:

<path_to_the_folder> – All the files in this path will be deleted irrespective of their extension or their age.

 

11 All Files Min

You can also try other combinations like deleting all the files that are of a specific extension by combining Sections 3 and 4. Please do let us know if you have any other custom requirements.

That’s it. Kindly tell us in the comments section whether you could automate the process with the help of our article.

Stay tuned for more amazing tips, tricks, how-tos, and hacks.