This post shows you how to create batch file to delete temp files on Windows 10 or 11 PC. We all know that temporary 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, eventually running out of disk space and slowing down your computer.
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 the 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 an 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.
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.