How to Clear Windows 11 Temp Files Automatically with a 1-Click Batch Script

Is your Windows 11 PC running slow? Is your C drive running out of space? Over time, Windows accumulates gigabytes of temporary data. These files clog your storage and drag down your system performance.

Instead of manually digging through hidden system folders, you can automate the entire cleanup process.

In this guide, we break down a powerful 1-click batch (.bat) script. It completely flushes your temporary files and reclaims your storage space instantly.

The Ultimate Windows 11 Temp Cleaner Script

Here is the exact code. It targets and wipes out the three heaviest clutter zones in Windows 11:

@echo off
:: Batch script to clear Windows 11 Temporary Files

echo Clearing User Temp files...
del /q /f /s "%temp%\*.*"
rd /s /q "%temp%"
mkdir "%temp%"

echo Clearing System Temp files...
del /q /f /s "C:\Windows\Temp\*.*"
rd /s /q "C:\Windows\Temp"
mkdir "C:\Windows\Temp"

echo Clearing Prefetch files...
del /q /f /s "C:\Windows\Prefetch\*.*"
rd /s /q "C:\Windows\Prefetch"
mkdir "C:\Windows\Prefetch"

echo Temporary files successfully deleted!
timeout /t 5

How It Works: Code Breakdown

If you are hesitant about running scripts on your machine, don’t worry. This script uses standard Windows commands. Here is exactly what each part does:

  • @echo off: Hides the raw background code lines from displaying while running. This keeps the command window looking clean.
  • del /q /f /s: This is the delete command. /q turns off confirmation prompts so it runs silently. /f forces the deletion of read-only files. /s deletes files from all subfolders.
  • rd /s /q: Removes the entire directory structure to catch locked folders.
  • mkdir: Instantly recreates a brand new, empty folder so Windows doesn’t run into errors later.

The 3 Targets:

  1. User Temp (%temp%): Stores junk from your web browsers, app installers, and discord/game caches.
  2. System Temp (C:\Windows\Temp): Stores temporary files generated by the Windows operating system itself.
  3. Prefetch (C:\Windows\Prefetch): Holds files that help apps launch faster initially, but it easily gets bloated with old data from apps you uninstalled months ago.

Step-by-Step Guide: How to Use the Script

Setting this up takes less than two minutes. Follow these simple steps:

Step 1: Create the File

Open your Windows Start Menu, search for Notepad, and open it. Copy the block of code above and paste it directly into the blank text document.

Step 2: Save as a Batch File

Click File in the top menu, then select Save As.

  • Change the Save as type dropdown box to All files (*.*).
  • Name your file ClearTemp.bat.
  • Choose your Desktop as the save location and click Save.

Step 3: Run with Administrator Privileges

Go to your desktop. Right-click on your new ClearTemp.bat file and select Run as administrator.

A black Command Prompt window will pop up. It will cycle through the folders and close automatically after 5 seconds.

💡 Pro-Tips and Troubleshooting

  • Access Denied Errors: If you see “Access Denied” errors in the black window, you forgot to right-click and choose Run as administrator. System folders require elevated permissions to clean.
  • Skipped Files: You will likely see messages saying certain files cannot be accessed or deleted. This is completely normal. It simply means those files are currently being used by an open app or a running background Windows service.
  • Safety: This script is 100% safe. It does not touch your personal photos, documents, or downloaded files.


Leave a Reply

Your email address will not be published. Required fields are marked *