• Setting up Disk Cleanup in Task Scheduler

    Home » Forums » Setting up Disk Cleanup in Task Scheduler

    Author
    Topic
    #494173

    I received an email request for these instructions. I considered that more folks might benefit if I answered in a post for everyone instead of an email to just one. So here goes.

    >>>Edit: Since this tutorial was first posted, some changes have been made in Windows 7 (through Windows Update/Update Rollup or some such) to the extent that the full listing of options won’t display unless the commandline is run in an elevated Command Prompt window – Right-click Command Prompt and select Run as administrator. This procedure works equally well in Windows 8/8.1 and Windows 10/10 AU.<<<

    To setup “WindowsSystem32Cleanmgr.exe /sageset:01” just type that line (without the quotes) on the Start Screen. It will open up Disk Cleanup Settings on the desktop. If you’re using a Start Menu with Widows 8 (or using Windows 7) type that line in the Search box or Run box and hit Enter.

    36720-Disk-Cleanup-Settings

    Choose the options you want, then click OK (in XP “Compress old files” is an option; don’t select that one – it will take forever on the first run). “Sage” is System Agent. There may be other Windows utilities that use it, but the “/sageset” switch is used to choose options/conditions and store them associated with a number. I use “/sageset:01” for Disk Cleanup. You can use any number up to 65535 (I think that’s the largest number it will recognize).

    To test it, on the Start Screen (or in the Run or Search box) type “WindowsSystem32Cleanmgr.exe /sagerun:01” (without the quotes) and hit Enter. It will jump to the desktop and run there. The “/sagerun” switch tagged with the correct number runs the options/conditions selected associated with that number.

    If that runs as you wish, you can create a desktop shortcut if you like by using that command line. Also, you can set it up to run automatically using Task Scheduler. Open Task Scheduler either logged into an account in the Administrators group, or right-click and select “Run as administrator”. It will open in the Task Scheduler Library.

    36721-Task-Scheduler-Library

    In the far right pane, click on “Create Task”, and a new task dialog box will open. In the General tab, use the settings as shown here. The dialog box won’t look exactly like this (more options will be available depending on which tab you’re on), but these are the settings to use:

    36722-Disk-Cleanup-Task

    You can name the task whatever you wish, and put in a description if you like. Next, click on the Triggers tab, and use the settings as shown here, just use whatever time you wish:

    36723-Triggers

    Next click on the Actions tab, and use these settings:

    36724-Actions

    Next click on the Conditions tab, and use these settings:

    36725-Conditions

    And finally, click on the Settings tab, and use these.

    36726-Settings

    If you have a question, post it in a reply, and I’ll try to answer it.

    Always create a fresh drive image before making system changes/Windows updates; you may need to start over!
    We all have our own reasons for doing the things that we do with our systems; we don't need anyone's approval, and we don't all have to do the same things.
    We were all once "Average Users".

    Viewing 4 reply threads
    Author
    Replies
    • #1447807

      Bruce,

      I have used & recommended this for many years. But, my command line is not the same as what you are suggesting. And, no, I’m not referring to 01 or up to 65535

      I used to use
      cleanmgr /sageset
      and
      cleanmgr /sagerun

      Later & ever since, use
      %SystemRoot%System32Cmd.exe /c Cleanmgr /sageset:65535 & Cleanmgr /sagerun:65535

      Which does the whole thing in 1 step instead of 2.

      The difference between your command line & the above are obvious.

      Maybe, you would comment…

      For the benefit of others who might not be familiar with commandline and/or Task Scheduler, I’ll stack the two so the differences are more obvious.

      C:WindowsSystem32Cleanmgr.exe /sageset:01
      %SystemRoot%System32Cmd.exe /c Cleanmgr /sageset:65535 & Cleanmgr /sagerun:65535

      In the beginning of yours, “%SystemRoot%” equates to “C:Windows” for most users. The “C:” can be replaced with whichever drive letter holds the Windows folder, if it is not on drive C, but they both do exactly the same thing, which is to begin a path statement.

      “System32Cmd.exe” continues the path statement and calls the executable “Cmd.exe” to run. This part of your commandline is not necessary. Cleanmgr.exe is a Windows System executable, will run directly under Windows, and does not have to be run under Cmd.exe. It will run via Cmd.exe, it’s just unnecessary.

      Which brings us to the “/c” switch. This switch instructs Cmd.exe to call another command specified by string, execute it, then terminate the command window. This is also unnecessary, as Cleanmgr.exe is a self-terminating executable, so calling Cleanmgr.exe directly will result in Cleanmgr.exe terminating itself once it has completed. As for the rest,

      To setup “WindowsSystem32Cleanmgr.exe /sageset:01” just type that line (without the quotes) on the Start Screen. It will open up Disk Cleanup Settings on the desktop. If you’re using a Start Menu with Widows 8 (or using Windows 7) type that line in the Search box or Run box and hit Enter.

      Choose the options you want, then click OK. The “/sageset” switch is used to choose options/conditions and store them associated with a number. I use “/sageset:01” for Disk Cleanup. You can use any number up to 65535 (I think that’s the largest number it will recognize).

      Once selected and stored, there is no need to run “/sageset” again, unless one wishes to change the stored options. So in your commandline, “Cleanmgr /sageset:65535” is no longer necessary after its first use, and can be omitted in all subsequent manual runs.[/SIZE]

      If one is going to do this via Task Scheduler does the command line need to be what you show in your instructions or can it be the line I have been using when doing this manually?

      As stated in my tutorial, the options one wishes to use for Disk Cleanup only need to be set once; they are stored via the associated number one has chosen, and are available for subsequent use by calling “/sagerun” together with the associated number. So if one uses “/sageset:01” to choose the options, “/sagerun:01” will use those same options every time Cleanmgr.exe is run in the future using that switch and number combination.

      So, for setting this up in Task Scheduler, just follow the tutorial.

      36724-Actions
      [/SIZE]

      And, btw, running it manually, the boxes do not have to be rechecked after the 1st time this clean-up is run. They will hold as initially selected in subsequent runnings. This is not the case for the conventional, built-in Disc Clean-up. Likely you are aware of this. Others may not be. But, as an IT Pro it’s my instinct to educate.

      Yes, as I stated, once “/sageset” has been run, the options are stored. The unnecessary “/sageset” switch in your commandline is also the reason your commandline will not work in Task Scheduler – it brings up the “Disk Cleanup Settings” dialog box, and while all the options are indeed pre-selected via the previous use of “/sageset:65535”, the dialog box won’t close; it must be OK’d.

      This is the reason your commandline will cause Task Scheduler to stall – it calls for user input, but Task Scheduler tasks run hidden, the dialog box will not be visible, and so it can’t be OK’d. Also, your commandline calls Cmd.exe first, then Cleanmgr.exe via the “/c” switch and the string. Since the dialog box is not visible, Cleanmgr.exe stalls, and since it can’t complete the string, Cmd.exe cannot close.

      Following the tutorial will work. The graphics are snipped directly from my Disk Cleanup task in Task Scheduler. It has run this way successfully for well over a dozen years. One may choose a different number for “/sageset” and “/sagerun”, or different Triggers, Conditions and/or Settings, but the Actions commandline needs to be entered as shown.

      Always create a fresh drive image before making system changes/Windows updates; you may need to start over!
      We all have our own reasons for doing the things that we do with our systems; we don't need anyone's approval, and we don't all have to do the same things.
      We were all once "Average Users".

    • #1447858

      I had not said the line I have been using would not work in Task Scheduler.

      The tutorial came about as a result of an email I received. Here’s a quote:

      I tried to set it up in Task Scheduler. Please, E-mail me the instructions on how to set up this Task.[/quote]

      Email or Private Message requests for assistance are in conflict with Rule 10, which states, in part: “Private requests defeat the purpose of the Lounge, which is designed to share information as widely as possible.”

      That’s why I posted this tutorial, to share this information as widely as possible.

      @ that point I had not tried it. Was trying to find out it HAD to be done w/ the line you showed. …Configuring Task Scheduler was not the issue.

      How the commandline is written in the Actions tab is a crucial part of configuring Task Scheduler. An incorrectly written commandline will cause the task to fail, but will not trigger any error messages as to the reason for failure being the commandline syntax.

      Your commandline is quite common. A Google search for that exact phrase yields about 405 results. (In the early days, the number 65535 was thought to have special significance; it doesn’t). For that reason, I explained in my reply to you how your commandline would cause Task Scheduler to stall, and the necessity for following the tutorial in order to achieve success.

      And the email request I received quoted that same commandline, hence the tutorial for setting up Disk Cleanup in Task Scheduler.

      Always create a fresh drive image before making system changes/Windows updates; you may need to start over!
      We all have our own reasons for doing the things that we do with our systems; we don't need anyone's approval, and we don't all have to do the same things.
      We were all once "Average Users".

    • #1447933

      Yes, it is common. It was shared w/ me & others by an IT colleague years ago, is how I came to know of it.

      I’ve been using it since my Windows 2000 Professional days; just never used the 65535, always 01, and only ran the /sageset switch once. I was a subscriber to The Langalist, and Fred wrote about it in 2002 – “Sageset Unlocks CleanMgr’s Power By Fred Langa on April 4, 2002 in LangaList Plus” (Fred used /sageset:99).

      I first set it up in Task Scheduler after I upgraded to XP. Here’s a link to a Windows Support article on the subject. “The n value can be any integer value from 0 to 65535.”

      Anyway, just to clarify… I wasn’t wanting to use that line but, wanting to know if the shorter line needed to be used in Task Scheduler.

      Perhaps your phrasing, “can it be the line I have been using”, through me off a bit:

      Maybe, you would comment… If one is going to do this via Task Scheduler does the command line need to be what you show in your instructions or can it be the line I have been using when doing this manually?

      The Actions tab graphic in the tutorial clearly shows “C:WindowsSystem32Cleanmgr.exe /sagerun:01”.

      At any rate, I’m glad you could learn something. That’s what the Lounge was designed for, “to share information as widely as possible.”

      Also, answering your questions has helped me to further clarify for other Lounge members and guests the use of Cleanmgr.exe and setting it up in Task Scheduler.

      You’re welcome, and thanks to you, as well.

      Always create a fresh drive image before making system changes/Windows updates; you may need to start over!
      We all have our own reasons for doing the things that we do with our systems; we don't need anyone's approval, and we don't all have to do the same things.
      We were all once "Average Users".

    • #1448174

      Looks like a good solution. Is this usable with any free disk cleanup tools?

      • #1448198

        Looks like a good solution. Is this usable with any free disk cleanup tools?

        It may be, but I would be hesitant to remove duplicate files. Windows uses a lot of duplicate files that are actually necessary for the OS. And anything that requires user intervention won’t work in Task Scheduler, as tasks run hidden, and will stall where user intervention is required.

        Always create a fresh drive image before making system changes/Windows updates; you may need to start over!
        We all have our own reasons for doing the things that we do with our systems; we don't need anyone's approval, and we don't all have to do the same things.
        We were all once "Average Users".

    • #1448362

      Clean-up on steroids in Task Scheduler is not stopping or shutting down. Started as scheduled but, every time I check just keeps indicating “running” even if it is several hours later or the next day.

      Open Task Scheduler, highlight your Cleanup task, right click and select properties. Check each tab against the graphics I used in my first post to make sure they match.

      Also, in the Actions tab, the Commandline “C:WindowsSystem32Cleanmgr.exe /sagerun:” (without the quotes) should be entered all in a single line in the Details box. The /sagerun switch should not be put in the “Add arguments (optional):” box.

      Always create a fresh drive image before making system changes/Windows updates; you may need to start over!
      We all have our own reasons for doing the things that we do with our systems; we don't need anyone's approval, and we don't all have to do the same things.
      We were all once "Average Users".

    Viewing 4 reply threads
    Reply To: Setting up Disk Cleanup in Task Scheduler

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: