Automatic deletion of the temp folder in Windows with a batch file.
In the Temp directory of Windows programs like to store files, which are needed for the operation of the program, but otherwise normally do not contain important data. For example, a program generates an HTML preview, the file is stored here and then opened in the browser.
Over time, many files can accumulate here, which occupy memory space and are not necessary. Windows itself brings two possibilities to delete the temp folder, the disk cleanup utility and the storage sense functionality. However, both options require some time to set these up. The disk cleanup utility must be run manually and the storage sense function runs from time to time and does not allow direct control.
So let’s use a different approach, The typical content of the directory looks like this.
Batch file to delete the temp directory
By means of a batch file you can also delete the directory regularly and quickly. Compared to disk cleanup, you save quite a bit of time here. The deletion takes only a few seconds with the batch file.
The contents of the batch file:
del /s /f /q %Temp%\*.* for /f %%f in ('dir /ad /b %Temp%\') do rd /s /q %Temp%\%%f
To do this, we copy the two lines into the Windows editor and save them with the file extension “.bat”.
If we run the batch file, all files and folders that can be deleted and are not currently in use will be deleted from the temp directory.
Execute automatically at system startup
One possibility is to empty the temp directory at system startup. To do this, we add the script to the Windows startup folder. Press CTRL + R and paste “shell:startup” into the dialog.
We now insert the batch file into the folder. The command will now be executed automatically at every system start.