How to Create Batch File to Delete Temp Files

This post shows you how to create batch file to delete temp files on Windows 10/11 PC. We all know that temp files occupy a lot of disk space on Windows and deleting them saves a lot of disk space. If you are looking to clean up your temp files using a batch file (.bat), this post is for you.

The term “temporary files” refers to files that are created either by Windows or user-installed programs. These files serve a dual purpose, assisting the computer in processing related programs or tasks and acting as a backup in case your system or program crashes unexpectedly.

The majority of temporary files are automatically removed after the task is completed, but some remain on your hard drive for future reference; if you do not delete them, they will continue to take up space on your hard drive and eventually you will run out of disk space and your computer will become even slower.

The temp folder is created for every user account that you create on a Windows PC. The temp folder can be accessed by navigating to the following path in the explorer: C:\Users\useraccountname\AppData\Local\Temp. You can also run a shortcut command %temp% to access the temp folder for your user account.

Create Batch File to Delete Temp Files

You can create a simple batch file to clear temporary files using the built-in Windows commands. Here’s an example:

@echo off
echo Clearing temporary files...

rem Clean up temporary files using built-in Windows commands
del /q /f %temp%\*.*
rmdir /q /s %temp%\

echo Temporary files cleared.

Save the above code as a .bat file (e.g., ClearTempFiles.bat) and run it. This batch file will delete all files in the %temp% directory and remove the directory itself. The /q flag is used for quiet mode, and the /f flag is used to force delete files. The /s flag is used to remove the directory and all its contents.

Please note that running this batch file will permanently delete the files in the temporary directory. Make sure you don’t have any important files in the temporary folder that you want to keep.

Also, keep in mind that some applications may be using files in the temporary folder, so running this batch file might interfere with those applications. It’s generally safe, but exercise caution.

Run the ClearTempFiles.bat to clean Temp Files

To run the batch file, launch the command prompt as administrator and navigate to the folder where you have saved the ClearTempFiles.bat file. Run the command ClearTempFiles.bat and this will clear all the temp files on your computer.

Create Batch File to Delete Temp Files
Create Batch File to Delete Temp Files

In the below screenshot, we see that all the temp files have been deleted. The script also deletes the temp folder but don’t worry, the folder will be automatically created upon a system restart.

Create Batch File to Delete Temp Files
Create Batch File to Delete Temp Files

Further Reading

Back to top button