Advertisement

Clean up temp directory automatically in Windows 10

Every Windows version uses a special directory which stores temporary files. These files are created by various Windows services, installed apps and tools. Temporary files can be safely deleted after the process which has created them has quit. However, this does not happen often, so the temporary directory continues to store them and fills your disk drive with junk. Here is how to clean up the temp directory automatically in Windows 10.

Advertisеment


There is a special environment variable in Windows 10, %temp%, which points directly to the directory with your temp files. You can test it in action by typing %temp% in the address bar of the File Explorer (%tmp% also works):Windows 10 file explorer address bar tempWindows 10 temp dir opened

Using this information, you can quickly create a batch file that will remove all the temp folder's content. If you put this batch file in your Startup folder, you'll get your Temp folder cleaned every time your start your PC.

Before you proceed, I suggest you to apply the following tweak: Add Batch file (*.bat) to New menu of File Explorer. You can save a lot of time by having the ability to create a new batch file directly.

Clean up temp directory automatically in Windows 10

  1. Create a new batch file with the following content:
    	@echo off
    	del "%tmp%\*.*" /s /q /f
    	FOR /d %%p IN ("%tmp%\*.*") DO rmdir "%%p" /s /q
    

    This will remove the contents of the Temp directory in Windows 10.Create clean temp batch file

  2. 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 to open the Startup folder directly.Shell startup in the run boxWindows 10 startup folder opened

  3. Move your batch file to the Startup folder and you are done!

Note: We intentionally did not delete the %temp% folder itself using batch commands because deleting the folder and recreating it can cause various permissions issues with the hundreds of apps that write to it. It is safer to delete the files inside it first and then the empty folders.

After doing the steps above, you can reboot your PC and open your Temp folder. You will find lesser files there than before. This will save you disk space and your time, since you need not clean the folder manually.

Update: Starting with build 15014, a new option appeared in Settings. Open Settings and go to System -> Storage. There, you will find the option named "Storage Sense". Enable it and you are done. Clear Temporary Files Automatically In Windows 10Windows  will remove all the temp folder's content automatically.

The user can customize this behavior. To do this, click the link "Change how we free up space" under the switch.

Change How We Free Up Space The related page will be opened: Change How We Free Up Space PageThat'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!

Advertisеment

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.

13 thoughts on “Clean up temp directory automatically in Windows 10”

  1. I’m not sure about doing that on startup. What if the reason you’re rebooting is to complete installation of a particular program, and what if that program is using %temp%? It’s probably better to put some logic in that only deletes things greater than 1 or 2 days old.

    1. At startup means AFTER reboot. So any app that requires reboot to finish and has placed temp files in %tmp% will not be affected. After a reboot, files generally are safe to clear unless the app requires multiple reboots to install.

      1. It happens sometimes during the startup routine, and an installation program that’s completing its installation then (because it needed a reboot to *continue* with its installation) could easily be utilizing files in %temp%.

        1. Hmm. Yes some installers do that. In that case, remove it from your Startup folder and add a Scheduled Task to run it whenever you want like when your PC is idle for 20 minutes or anything you want.

  2. Great tip!

    But, as i understood, putting batch to startup will result in it’s executing after explorer and all other apps have already started and created their files in temp. So, script won’t be able to wipe it properly.

    I suggest adding it to registry /run/ to execute it as early while boot as it can be done.

    Maybe optimally, after logon, but before explorer started.

    1. %tmp% directory in Windows is never completely clean, some process is always writing to it and it’s not recommended to delete in-use files anyway or delay logon for purpose of cleaning it. It will clean whichever files are not locked. The purpose of this script is not to completely wipe %tmp% to 0 bytes and keep it empty but to clean up files which the Disk Cleanup utility misses out on.

  3. Hi all,

    in my opinion, that way is much faster:

    FOR /d %%p IN ("%tmp%\*") DO rmdir "%%p" /s /q
    del "%tmp%\*" /q /f

    script don’t search the dir tree twice, and search for file name: * (eg. 12345678 & 12345678.123). You use: “*.*”, which means filename 12345678.123, but not 12345678, for example.

    Thanks for the great work and time for the author of the page.

    1. Yep it works faster.
      BUT.

      Note: We intentionally did not delete the %temp% folder itself using batch commands because deleting the folder and recreating it can cause various permissions issues with the hundreds of apps that write to it. It is safer to delete the files inside it first and then the empty folders.

      That’s it.

      1. Hi,

        i heave never try to delete %tmp%, or %temp% folder, but I use user %tmp% & %temp% directory in memory (which is created on every windows start-up), that’s much faster and i have never had problems with this. I use special (private) script in RunOnce, which deletes all user and windows “tmp” folders.

        If You need my RunOnce script, send me e-m@il, but it’s not for ALL (it’s mine, and not open or public)

        P.S. Please make my comments public

  4. How about cleaning files in C:\Windows\Temp? What syntax would it be?

    @echo off
    del “%tmp%\*.*” /s /q /f
    FOR /d %%p IN (“%tmp%\*.*”) DO rmdir “%%p” /s /q

    del “C:\Windows\Temp*.*” /s /q /f
    FOR /d %%p IN (“C:\Windows\Temp*.*”) DO rmdir “%%p” /s /q

  5. I think my way is simpler:
    1- run “shell:startup” to see startup folder of current user
    2- create a shortcut to cmd.exe
    3- do a right click on shortcut and change the properties
    set target command as below:
    %windir%\system32\cmd.exe /c rmdir /s /q “%temp%\” > nul 2>&1
    click on ok and done

Leave a Reply

Your email address will not be published.

css.php
Using Telegram? Subscribe to the blog channel!
Hello. Add your message here.