How do I auto-delete temporary files on Windows 11?

A two-line bat script plus a Task Scheduler entry clears Windows 11 temporary folders on its own, every single day.

Masadaki dizüstü bilgisayarın ekranında Windows 11 görev zamanlama penceresi ve yanında açık bir metin düzenleyici görünüyor.

Short answer

Open Notepad and write a small bat script with two del commands: one for your user temp folder (%TEMP%) and one for C:\Windows\Temp. Save it as ClearTempFiles.bat, then open Task Scheduler, use "Create Basic Task" to schedule it daily, point the action at that file, and turn on "Run with highest privileges" so the system folder can be cleared too.

What you need

A PC running Windows 11
A user account with administrator rights
Notepad (ships with Windows)
A permanent folder for the script, for example C:\Scripts

Step by step

01

Write the cleanup script

Press Windows+S, search for Notepad and open it. In the blank document type three lines: @echo off, then del /s /f /q %TEMP%\*.* >nul 2>&1, then del /s /f /q C:\Windows\Temp\*.* >nul 2>&1. The first line hides the command output; the other two empty your account's temp folder and the system-wide temp folder.

02

Save the file as a bat

In Notepad choose File > Save as. Set the "Save as type" menu to "All Files" and name the file ClearTempFiles.bat. Without the .bat extension the file stays plain text and will never run. Save it somewhere you will not delete by accident, such as C:\Scripts, instead of the desktop.

03

Create the task

Press Windows+S, find Task Scheduler and open it. Click "Create Basic Task" in the right-hand Actions pane. Type a name you will recognize later, such as Clear Temporary Files, then click "Next".

04

Set the schedule

On the trigger screen select "Daily" and click "Next". Fill in the date and time in the "Start" field. Pick an hour when you are not at the machine, for example 3:00 AM, so the script is less likely to touch files that open programs are still using.

05

Point the action at the script

On the action screen choose "Start a program" and click "Next". In the "Program/script" box type the full path to the script, for example C:\Scripts\ClearTempFiles.bat. If the path contains spaces, wrap it in quotation marks, then click "Next" to reach the summary.

06

Grant administrator rights

On the final screen tick "Open the Properties dialog for this task when I click Finish" and press "Finish". In the window that appears, enable "Run with highest privileges" near the bottom and click "OK". Without this permission C:\Windows\Temp cannot be cleared.

07

Test the task by hand

Find the task in the Task Scheduler Library, right-click it and choose "Run". If "Last Run Result" in the lower pane shows 0x0, the task completed without errors. To pause it later use right-click > "Disable", or "Delete" to remove it entirely.

08

Widen the scope if you want

To empty the Recycle Bin too, add the line PowerShell.exe -Command "Clear-RecycleBin -Force". For the Windows Update cache, add net stop wuauserv, then rd /s /q C:\Windows\SoftwareDistribution\Download, then net start wuauserv, in that order. Run the script manually once after every addition to confirm nothing breaks.

Ad — in-article responsive

Tips

If you would rather not write a script at all, turn on Storage Sense from Settings > System > Storage. It is less flexible, but it automates cleanup with a single toggle.
To run the script at sign-in, open the task's Properties > Triggers tab, click "New", and pick "At startup" from the "Begin the task" menu.
Running a script at shutdown needs the Local Group Policy Editor (gpedit.msc), which Windows 11 Home does not include. On Pro, follow Computer Configuration > Windows Settings > Scripts.
Adding a line that clears C:\Windows\Prefetch can slow the first launch of your programs for a while, so run that occasionally rather than every day.

Watch out

Temp folders hold files that open programs are actively using. Some files will refuse to delete while the script runs, which is normal, but running it with unsaved work open can cost you data. Never hand-edit the C:\Windows\Temp path to point somewhere else: a wrong path combined with a recursive delete command produces damage you cannot undo.

Frequently asked questions

+The script says it could not delete some files. Is something wrong?

No. Those files are locked by programs that are currently running. The script deletes everything else, and the locked files get cleared on a later run.

+Can this cleanup break my programs?

Temp folders exist for disposable data, so no. Cleaning up during an active installation or with unsaved work open can still cause errors, which is why an overnight schedule is safer.

+What is the difference between Storage Sense and the script?

Storage Sense only clears the temporary files and downloads that Windows recognizes, on its own interval. With the script you decide exactly which folder gets cleared and when.

+How do I check whether the task is actually running?

Select the task in the Task Scheduler Library and look at the "Last Run Time" and "Last Run Result" values at the bottom. A result of 0x0 means the run succeeded.

+Do I have to run the script as administrator?

Not for your own %TEMP% folder, but it is required for system folders like C:\Windows\Temp. That is why "Run with highest privileges" must stay enabled on the task.

Sources and verification

Last verified: 12 Eylül 2026. tell us. Menu names follow the English Windows 11 interface; localized builds use translated labels in the same locations.

This content is for information only; for official procedures the relevant institution’s current announcements take precedence. Details: disclaimer.

N?
Nasıl Yapabilir? Editör Ekibi

Resmî kaynak esaslı, her yayında iki editör onayından geçen rehberler. Our editorial principles →