As you use your Windows 11 PC, applications constantly generate temporary files. Installers unpack data, web browsers cache images, and Word creates auto-recovery snapshots. While Windows is supposed to delete these files when they are no longer needed, it often fails to do so, leaving gigabytes of digital clutter hidden deep within your AppData folder. Instead of manually running the Disk Cleanup tool every few weeks, you can create a simple batch script that automatically purges the Windows Temp folder every time you start your computer.
How to Create the Cleanup Script
We will use the native command prompt to forcibly delete all files and folders located within the designated Windows temporary directory.
- Right-click on an empty space on your Windows 11 desktop, select New, and click Text Document.
- Open the newly created text file in Notepad.
- Paste the following commands into the document exactly as written:
rd %temp% /s /q md %temp% - Click on File in the top left corner, then click Save As…
- In the Save As dialog box, click the Save as type dropdown and change it from Text Documents (*.txt) to All Files (*.*).
- Name the file
CleanTemp.bat(the.batextension is required to make it an executable script) and save it to your desktop.
Understanding the Commands
The first line (rd %temp% /s /q) tells Windows to Remove Directory (rd). The %temp% variable specifically targets your user account’s temporary folder (usually located at C:\Users\YourName\AppData\Local\Temp). The /s flag tells it to delete all subfolders, and the /q flag tells it to do so quietly without asking you “Are you sure?” for every single file.
Because the first command completely deletes the Temp folder itself, the second line (md %temp%) Make Directory instantly recreates a fresh, empty Temp folder so your applications have a place to write data when they launch.
How to Run the Script on Startup
Now we just need to place this script in the correct folder so Windows 11 executes it during the boot process.
- Press the Windows Key + R to open the Run dialog box.
- Type
shell:startupand press Enter to open your personal Startup folder. - Click and drag the
CleanTemp.batfile from your desktop into the Startup folder.
The next time you restart your PC, you will briefly see a black command prompt window flash on your screen for a fraction of a second. This is your script silently clearing out the junk data in the background, ensuring you always boot into a clean system with maximum available storage space.