Clear temporary files during system shutdown
No, restarting or shutting down a Windows computer does not clear temporary files. Files in the %temp% folder and other temp locations stay on the drive until a cleanup tool removes them. On Windows 11 and Windows 10 you can automate that with Storage Sense, or run Disk Cleanup with a saved profile on every shutdown through a Group Policy shutdown script, as described below.
Leftover temporary files cost disk space and can reveal what you worked on to anyone with local access to the PC, which is why automatic cleanup is worth setting up.
Clean temporary files automatically with Storage Sense
Storage Sense is the built-in Windows 11 and Windows 10 feature that deletes unnecessary temporary files automatically. Turn it on under Start > Settings > System > Storage by setting Storage Sense to On.
Temporary file cleanup is enabled by default once Storage Sense runs. In its settings you can also empty the Recycle Bin after a set number of days and, optionally, delete files in Downloads that have not been opened for a while. By default Storage Sense runs when the device is low on disk space; the "Run Storage Sense" option lets you pick a regular schedule instead.
Storage Sense has limits: it only cleans the system drive (usually C:), and it only runs when you are signed in and online for more than 10 minutes, according to Microsoft's Storage Sense documentation. It does not run at shutdown. For cleanup tied to shutdown, use the method in the next section.
For a one-off cleanup, press Windows+R, type %temp%, select everything in the folder and delete it. Skip any file that Windows reports as in use.
Run Disk Cleanup on every shutdown with Group Policy
Disk Cleanup can run a saved cleanup profile from the command line, and a Group Policy shutdown script can start it each time the PC shuts down. The cleanmgr command reference lists Windows 11 and Windows 10 as supported.
- Run
cleanmgr /sageset:1from an elevated Command Prompt or the Run box. The number can be anything from 0 to 9999; it names the profile. - In Disk Cleanup Settings, check the items to delete (for example Temporary files and Temporary Internet Files) and click OK. Select an item to read its description if you are unsure.
- Create a text file named
cleanup.cmdthat contains the linecleanmgr /sagerun:1. - Open the Local Group Policy Editor with
gpedit.msc. - Go to Computer Configuration > Windows Settings > Scripts (Startup/Shutdown) and double-click Shutdown.
- Click Add, then Browse, select
cleanup.cmdand confirm with OK, Apply and OK.

The screenshot shows the Disk Cleanup Settings dialog from 2007; the current dialog works the same way. Shutdown scripts run as Local System, so they can clean system-wide locations. The 2007 version of this guide used a logoff script under User Configuration instead, which still works but runs with your user rights only.
The Group Policy Editor is only included in Pro, Enterprise and Education editions. On Home editions, create a Task Scheduler task that runs cleanmgr /sagerun:1 at log on or on a schedule.
Related: Windows shutdown command options, enable the Group Policy Editor on Windows 11 Home, privacy.sexy cleanup and privacy scripts
Thanks for procedure to delete tmp files at shudown, it was great.
Best wishes for New Year
Harry
This thing makes shutdown take forever. Stick with the a batch file.
I’d tried a very similar method a while ago – though that one used a batch file with the following contents:
RD /S /Q %TEMP%However, that didn’t really work perfectly (there were always a few files and folders left after a reboot), so I will give this a try now.