Last week I explained how to schedule the Disk Cleanup utility to perform basic maintenance tasks. But the Disk Cleanup utility can be overkill, and in some cases you might not want it to run through all its options. How can you tell it you just want it to just clean out the Windows Temp folder? The answer is found in some amazingly useful command-line switches that most Windows users know nothing about. (They’re documented in Windows XP Inside Out and in Knowledge Base article 315246, How to Automate the Disk Cleanup Tool in Windows XP.)
To control how the cleanup process works, you need to first know the executable command for the Disk Cleanup utility, cleanmgr.exe. Then you need to learn to use the following switches to modify its behavior:
- /D driveletter Runs Disk Cleanup using the drive letter you specify in place of driveletter (for example, type cleanmgr /d c:, to apply the utility to drive C). (This option is ignored if you use the /Sagerun switch.)
- /Sageset:n Opens a dialog box that lets you select Disk Cleanup options, creates a registry key that corresponds to the number you entered (where n is an arbitrary number from 0 through 65535), and then saves your settings in that key.
- /Sagerun:n Retrieves the saved settings for the number you enter in place of n and then runs Disk Cleanup without requiring any interaction on your part.
To put all the pieces together, follow these steps:
- Click Start, Run (or open a Command Prompt window) and type the command cleanmgr /sageset:200. (This number is completely arbitrary; you can choose any number from 0 through 65535.)
- In the Disk Cleanup Settings dialog box, choose the options you want to apply whenever you use these settings. In this example, I’ve cleared all the check boxes except the one in front of Temporary files.

- Click OK to save your changes in the registry.
- Create a shortcut that uses the command cleanmgr /sagerun:200. Save the shortcut in the Windows folder using a descriptive name – Clean Temp Files, for instance.
- Open Control Panel’s Scheduled Tasks folder and start the Add Scheduled Task Wizard. When prompted to select the program you want Windows to run, click Browse and select the shortcut you just created. Follow the wizard’s remaining prompts to schedule the command to run at regular intervals.
- Repeat steps 1–5 for other Disk Cleanup options you want to automate.
That’s it. As long as the Task Scheduler service is running, your cleanup chores will be handled automatically on the schedule you specified.
Ed:
I set this up exactly as instructed. It runs automatically until it displays the options for deleting files. I then must manually select “OK” before it actually deletes the files.
Is there a way to configure it so that it automatically deletes the preset sagerun files? Or am I doing something wrong? I have multiple partitions on the physical drive containing the C drive I’m trying to automate.
Thanks
Ken
Ed, never mind. I think I figured out what I did wrong. Only now it is automatically cleaning all of the partitions even though I used the /d c: switch. [That’s okay.]
Thanks
Ken
Ken, the /d switch is ignored if you use /sagerun. I updated the post to add this detail.
To stop the Disk Cleanup wizard taking a year to check whether it can compress old files, follow the instructions in KB article 812248 (http://support.microsoft.com/kb/812248). This turns off the Compress Old Files feature.
Is there a subtle reason why you set up a shortcut for sagerun – rather than simply including the switch in the (advanced) Scheduled Task configuration?
Unkamunka, the main reason is so that the shortcut could be reused in other contexts. Without this step, you would have to open the Scheduled Tasks folder (or remember the exact command line switch) to use your customized Disk Cleanup task.
But you are correct that using the sagerun switch directly in the Scheduled Tasks window is another way to accomplish the same task.
Ed:
I set this up exactly as instructed. It opens automatically at the preset time but I have to make my selections what I want cleaned, and then I would have to click OK.
It’s not opening and cleaning the Temp folder automatically.
Ed, I want to know what do I need in my batch file to stop the sagerun from doing disk clean up on all partitions. I just want it to work on the C drive. How can I do that?
Raj, the /driveletter switch and the /sagerun switch do not work together, unfortunately. So if you want to automate things you have to do so for all drives, not just one.
So if I don’t use sagerun will I be able to just clean up the 1st partition.
Yes. When you start the wizard, you pick a disk.
Sorry what I mean is I have a hard drive with 3 partitions. I have created a batch file so that disk clean up can work with no user interaction. I have done this by using your instructions above with sagerun. Then I scheduled the batch file to run when I log on.
It all good until it starts cleaning all the partitions. So my question really is:
Is there a way I can automate disk cleanup in a batch file for just the C Drive with no user interaction.
I can appreciate that it will not be using sagerun.
Sorry for the long history but I am willing to learn new things and enjoy programming if that helps. Your help is greatly appreciated. Thank you.
Raj, if you use the /sagerun switch in a batch file (or at the command line) its settings apply to all available volumes. The only way to automate the disk cleanup wizard for a single drive is to set a scheduled task that applies all cleanup tasks to a selected drive letter using the /d switch. So it’s one or the other.
When disk cleanup starts automically? (What is the size of disk space when it starts? Could it be changed/customized? – for example that disk cleanup starts or alert me when empty disk is 50Mb not before)
I have used SageRun in a batch file under XP Pro and it always seems to run 2 instances of Cleanmgr.exe Why? I’ve tried the one liner:
cleanmgr /sagerun:01
and this longer batch
cleanmgr /sagerun:01
goto end
:end
EXIT
Both run the cleanmgr.exe twice!
NEVER MIND.
It runs twice because I have two partitions and as Ed Bott said above “if you use the /sagerun switch in a batch file (or at the command line) its settings apply to all available volumes”
Is there a way to automate scheduled tasks on Win 2000/2003 server?
I’ve created two scheduled tasks using the Task wizard:
1. Disk Cleanup, this waits for a user to click OK before it will perform the task.
2. Disk Defrag, waits for a user to pick what drive (partition) I want to defrag.
I would like to schedule these tasks to run automatically overnight without user intervention.
Do I need to add any quotes or switches to the Run line for the Scheduled Task?
This does not seem to be User Account related
Hi,
I followed these directions, but when the taks runs, it prompts for me to choose a drive. I thought you said it would run on all drives. How can i get around the prompt so it just runs.
Hi – I’m hoping you can assist with another problem. I’m running the Disk Cleanup pretty regularly but I’m concerned about the compressed filed. For quite some time now, the results are the same. 3,126, 154 (give or take). I say “OKay” but it doesn’t compress anything. I still get the message that I will clear up that much disk space but when I run it again, it stays the same. HELP HELP ME HELP!
Hello, I followed the directions in the article. I am preparing a test automation to deploy to other machines. I am testing on my laptop that has one drive and volume which is c:. I typed cleanmgr /sageset:500 and then selected the options I wanted. I then created a shortcut in the windows folder using cleanmgr /sagerun:500 and named it clean500. I then scheduled the task to run within a few minutes. The problem is that I am still getting prompted to click ok after it scans and before it executes the program. Is there something I am not doing correctly. Thanks for the help in advance.
Hi, I’m very interested in the topic dicussing. I actually having a similar problem, where I wish to setup or should I say add another task in a scheduled tasks that I setup previously. And the scheduled task should be running on multiple task. I know this can be done on Control Panel|Scheduled Tasks|Add Scheduled Task, but I wish to do it using command prompt, but not very sure how to do it. I try to check at /? using command prompt, but wasn’t helping. Please anyone can tell me how to do this? Thank you. My OS is running on XP with password protected.
I have been trying to locate a method on using the cleanmgr /sagerun:# command in a batch file It isn’t that I can’t do it using in on a single account that I am working from but rather doing it as the administrator and running it on the other accounts while in the administrator’s account. Can you help?