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.


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 temp Windows 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 box Windows 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.

9 thoughts on “Clean up temp directory automatically in Windows 10

  1. Rick

    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.

    Like or Dislike: Thumb up 0 Thumb down 0

    Reply
    1. Bob

      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.

      Like or Dislike: Thumb up 0 Thumb down 0

      Reply
      1. Rick

        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%.

        Like or Dislike: Thumb up 0 Thumb down 0

        Reply
        1. Bob

          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.

          Like or Dislike: Thumb up 0 Thumb down 0

          Reply
  2. Toshik

    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.

    Like or Dislike: Thumb up 0 Thumb down 0

    Reply
    1. Bob

      %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.

      Like or Dislike: Thumb up 0 Thumb down 0

      Reply
  3. PiR2

    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.

    Like or Dislike: Thumb up 0 Thumb down 0

    Reply
    1. Sergey Tkachenko Post author

      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.

      Like or Dislike: Thumb up 0 Thumb down 0

      Reply
      1. PiR2

        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

        Like or Dislike: Thumb up 0 Thumb down 0

        Reply

Leave a Reply

Your email address will not be published. Required fields are marked *