Clear Temp Directory on Startup with Disk Cleanup

Recently, we saw how to clean up the temp directory automatically in Windows 10 using a simple batch file. In this article, we will see how to perform the same task using the built-in Disk Cleanup tool (cleanmgr.exe). Unlike the batch file, it shows a progress window with messages and a progress bar which gives you an idea of how long the operation will take.

As you may already know from the earlier article on how to "Clean up temp directory automatically in Windows 10", this is a special directory in Windows where the operating system stores temporary files. Temporary files are supposed to be deleted after the process which has created them has quit. However, this does not happen often. This causes the temporary directory to store them and grow in size filling your disk drive with files that you do not need.

You can check the current temp directory's contents using a special environment variable, %temp%(%tmp% also works). It points to the directory with temp files. Just type "%temp%" or "%tmp%" without quotes into the address bar of File Explorer:
The Disk Cleanup app (Cleanmgr) is a built-in Windows tool which can remove various junk files to free up disk space. It can remove old Windows Update files, the browser cache, Recycle bin files and many other items all at once. One of its options is the ability to clean up the temporary directory. This is exactly what we need.

Disk Cleanup supports presets via a special pair of command line arguments, /SAGESET and /SAGERUN. You can create a preset to remove only the temp directory's contents and put a shortcut in your startup folder. So every time you sign in to your user account, the temp directory will be cleaned automatically. I suggest you to read the following articles before continuing:

The second article describes the idea behind the trick that we will use today.

Clear Temp Directory on Startup with Disk Cleanup

  1. Open an elevated command prompt.
  2. Type the following command
    cleanmgr.exe /SAGESET:number

    The number is a unique identifier for your preset. Think of it as the name of your preset. It can be any value from 0 to 65535.
    Suppose you use the number 100, for example:

  3. Tick only one option "Temporary files" to store it as your preset "100":
  4. Click OK to save the preset under the number you entered from the elevated command prompt.

Now, let's create a shortcut for the preset you created.

  1. Right click the empty space on your Desktop and select New - Shortcut from the context menu.
  2. In the shortcut target box, type the following command:
    cleanmgr.exe /SAGERUN:100

    Use the same number instead of 100 which you used with the SAGESET switch.

  3. Name your shortcut as "Clear Temp Directory".
  4. You can change the shortcut icon if you want.

Voila, the shortcut is created. Every time you click it, it will empty your temp directory directly.

Now, lets clear the temp directory on every startup.

Clear Temp Directory on Startup with Disk Cleanup

  1. Press Win + R shortcut keys together on the keyboard to open the Run dialog. Type or paste the following in the Run box:
    shell:Startup

    The text above is a special shell command which makes File Explorer open the Startup folder directly.

  2. Copy or move the shortcut you just created to the Startup folder and you are done!

From now, the Temp directory will be clean every time you sign in to your user account.

That's it.

Support us

Winaero greatly relies on your support. You can help the site keep bringing you interesting and useful content and software by using these options:

If you like this article, please share it using the buttons below. It won't take a lot from you, but it will help us grow. Thanks for your support!

Author: Sergey Tkachenko

Sergey Tkachenko is a software developer who started Winaero back in 2011. On this blog, Sergey is writing about everything connected to Microsoft, Windows and popular software. Follow him on Telegram, Twitter, and YouTube.

One thought on “Clear Temp Directory on Startup with Disk Cleanup”

  1. more cr@p to remove from a growing list of things to disable and remove! plus it slows your system down and reduces freedom of choice to configure your security as you wish!

Leave a Reply

Your email address will not be published.

Exit mobile version
Using Telegram? Subscribe to the blog channel!
Hello. Add your message here.