The TEMP directory (
%temp%) gets bloated over time as applications store data over there and don’t always remove the files and folders they created. And, software installers extract the contents of the setup packages to the %temp% folder and fail to do the cleanup act when installation is complete.
“Low disk space” problem is a thing of the past, or seldom occurs nowadays as systems come with high-capacity hard drives which became dirt cheap. But, you may still want to tidy up the Temp folder and keep the system in top shape.
Temporary files cleanup is one of the tasks which Windows performs silently in the background, as a part of automatic maintenance or system maintenance which uses the Task Scheduler. Automatic maintenance uses Disk Cleanup (cleanmgr.exe) with the undocumented
/autoclean parameter. The built-in maintenance tasks trigger if the system is left idle for about 5 minutes or so, and stops running as soon as Windows senses user activity.
To clear the %temp% files instantly without waiting for the automatic or system maintenance OS task to fire, you can use one of these methods.
Methods to cleanup Temp files
- Automate temporary files removal using Disk Cleanup
- Automate temporary files removal using Storage Sense [Windows 10 Settings]
- Automate temporary files removal using a Batch file
The native Disk Cleanup (cleanmgr.exe) utility supports automation since the early versions of Windows, at least from Windows XP, and up until Windows 10. You can use the “sageset” and “sagerun” commands with a unique number to configure and run the cleanup automatically.
- /d driveletter: – This option specifies the drive that you want Disk Cleanup to clean.
- /sageset: n – This option displays the Disk Cleanup Settings dialog box and also creates a registry key to store the settings that you select. The n value, which is stored in the registry, allows you to specify tasks for Disk Cleanup to run. The n value can be any integer value from 0 to 65535. To have all of the options available when you use the /sageset option, you might need to specify the drive where Windows is installed.
- /sagerun: n – This option runs the specified tasks that are assigned to the n value if you use the \sageset option. All drives on the computer are enumerated and the selected profile runs against each drive.
For example, you can use
/sageset:1000 for cleaning
/sageset:1001 for other items such as “Thumbnails” cache, and so forth. Also, you can select multiple cleanup options for a single
sageset # configuration.
Configuring Disk Cleanup for Temp Files Cleanup
Open an elevated or Admin Command Prompt and run the following command:
cleanmgr /d c: /sageset:1000
This opens Disk Cleanup, showing you an overwhelming list of cleanup options or handlers to choose.
- Temporary Setup Files
- Old Chkdsk files
- Setup Log Files
- Windows Update Cleanup
- Windows Defender
- Windows upgrade log files
- Downloaded Program Files
- Temporary Internet Files
- System error memory dump files
- System error minidump files
- Files discarded by Windows upgrade
- Per user archived Windows Error Reporting Files
- Per user queued Windows Error Reporting Files
- System archived Windows Error Reporting Files
- System queued Windows Error Reporting Files
- System created temporary Windows Error Reporting Files
- Windows ESD installation files
- Previous Windows installation(s)
- Recycle bin
- RetailDemo Offline Content
- Update package Backup Files
- Temporary Files
- Temporary Windows installation files
- User file history
Some of the cleanup options may not exist in your system depending upon the Operating System you’re using.
“Downloaded Program Files” and “Temporary Internet Files” options may be checked by default. Uncheck them, enable “Temporary Files” checkbox and click OK.
You need to run the “/sageset” only for the first time. The configuration (for 1000) gets saved in the registry, and is used when you run Disk Cleanup cleanup using the “/sagerun” command. You can create as many configuration settings with different numbers, as you’d like.
Running Temporary Files cleanup
To run the cleanup, use this command from the Run dialog (WinKey + R):
You don’t require the
/d parameter when using
Create a shortcut for the above command, and place it in the Startup folder.
Press WinKey + R, type the following command and press ENTER:
That opens the Startup folder of your profile. Place the Disk Cleanup shortcut into the Startup folder.
%temp% files cleanup will occur at every login.
Note: By default, Cleanmgr.exe won’t delete temp files which have been accessed in the last 7 days. However, you can change its behavior by customizing the LastAccess registry value. For more information, check out article Why doesn’t Disk Cleanup Wipe Out the Temp Folder Contents Completely?
In Windows 10 Creators Update, you can let Windows automatically clear the temporary files and Recycle Bin files that are older than 30 days. You can enable the option via Settings.
1. Click Start, Settings [or press Winkey + i]
2. Click System, and then select Storage
3. Under Storage Sense, enable the toggle switch under “Windows can automatically free up space…”
Also, you can choose what’s cleared up automatically – Temporary files or Recycle Bin — or both. Click “Change how we free up space.”
You can enable or disable the following settings in that page
Delete temporary files that my apps aren't using. Delete files that have been in the recycle bin for over 30 days
You can also cleanup files on demand, by clicking the “Clean now” button under “Free up space now”.
Automatic free up of disk space is an useful feature introduced first in the Creators Update preview build 15014, and would be available in the Windows 10 Creators Update final version.
You can make a Batch file to wipe out the contents of the Temp folder completely including the subfolders.
Open Notepad and copy the following contents into it:
del "%temp%\*.*" /s /q
FOR /d %%p IN ("%temp%\*.*") DO rd "%%p" /s /q
Save the file with the
.bat extension. Double-click to run the Batch file. It doesn’t remove the Temp folder itself as it’s not recommended to do so.
Editor’s note: Using Disk Cleanup is the safest and preferred option, as it leaves untouched the recently accessed files. Also, it allows the user to configure the “last access” setting, albeit only using the registry. But if you prefer the Batch file method (which I don’t recommend for routine use) for some reason, make sure you save your open files, and close all your running programs before running the file.
About the author
Ramesh Srinivasan founded Winhelponline.com back in 2005. He is passionate about Microsoft technologies and has a vast experience in Windows — delivering support for Microsoft's consumer products. He has been a Microsoft MVP (2003-2012) who contributes to various Windows support forums.