Automated cleanup of Temporary files, Internet files, downloaded files, recycle bin.
Syntax CLEANMGR [/d driveletter] option CLEANMGR [/d driveletter] /sageset:n option CLEANMGR [/d driveletter] /sagerun:n option Options /d driveletter: Select the drive that you want Disk Cleanup to clean. /sageset:n Display the Disk Cleanup Settings dialog box and create a registry key to store the settings you select. The n value is stored in the registry and allows you to specify different tasks for Disk Cleanup to run. n can be any integer from 0 to 65535. Specify the %systemroot% drive to see all the available options. /sagerun:n Run task 'n' All drives in the computer will be enumerated, and the selected profile will be run against each drive. /TUNEUP:n Undocumented, appears to be the same as /sageset /LOWDISK Disk Cleanup will open with all options checked by default. Normally invoked when Windows notifies that of disk space is running out. /VERYLOWDISK Run with the default settings, no user prompts. /SETUP Analyze the system files left over from a previous Windows version. [Undocumented] /Autoclean Analyze the system files left over from a previous Windows version/in-place upgrade. and also remove those files automatically. [Undocumented] /Help Display help /Usage Display help /? Display help
/sageset and /sagerun cannot be combined in a single command.
If CLEANMGR is run without /sageset or /sagerun, it will start by calculating how much disk space could be saved. This makes the process much slower to run.
Some cleanup options require elevation, when run without elevation the GUI will include an option [Clean up System Files] which will relaunch CLEANMGR in Elevated mode.
Choose a set of options for cleanup (this will display the GUI) and Save as settings group #64:
Temporary Internet Files *
Temporary Setup Files
Downloaded Program Files *
Old Chkdsk Files
Recycle Bin *
Temporary Offline Files
Compress Old Files
Catalog Files for the Content Indexer
System files: Debug/chkdsk/Installer/Memory dump/Windows update/error reporting logs.
Items in bold can appear in more than one drive, the items marked with * can be viewed in the GUI.
Run the saved settings group #64
To run Disk Cleanup this way regularly, you can create a shortcut which can be run from the Windows Startup folder or via a regular schedule. To run the saved settings group #64
%systemroot%\system32\cmd.exe /c CLEANMGR /sagerun:64
Many files in Application Data hold system data that should not be deleted, however some applications do leave files which you can delete from a roaming profile, these can be selectively removed with a VBScript like this.
The 'User Profile/Recent' folder (for Start, Documents) can contain many more shortcuts than are set to display in the GUI. A very large number of these can affect logon/logoff times.
To clear out the shortcuts:
echo y| del *.*
Close all applications
Open a command prompt
taskkill /im explorer
This should kill explorer and bring up a logout/shutdown dialogue box.
Simultaneously press CTRL+SHIFT+ALT.
While you keep these keys pressed, click [Cancel] in the Shut Down Windows dialog box.
In the command prompt window, you should now be able to delete the locked files.
At the command prompt, type explorer, and press ENTER to restart Windows Explorer.
To enable Cleanmgr on Windows 2008, open Server Manager and choose Add feature, then select "Desktop Experience"
After running cleanmgr on a server you will probably want to disable "Desktop Experience" again (or use this script.)
When necessary cleanmgr can take ownership of the files before deleting them.
Registry settings for CLEANMGR are held in:
Log files for CLEANMGR are stored in:
”Mrs. Joe was a very clean housekeeper, but had an exquisite art of making her cleanliness more uncomfortable and unacceptable than dirt itself“ ~ Charles Dickens
DELPROF - Delete user profiles and/or User Profile cache.
DEFRAG - Defragment hard drive.
Q253597 - Automating Disk Cleanup in Windows.
Equivalent bash command (Linux):watch - Execute/display a program periodically.