Batch script to delete temp files

Normally I would send an email about once a month, if I remember, to tell people to use the disk cleanup tool so that the temporary folders don’t get too bloated.  But yesterday, I discovered that it apparently isn’t emptying the windows temporary folder.  I guess I incorrectly assumed that was part of what it was doing.  I had 2 computers with windows\temp folders over 30 gigs.

This prompted me to hunt for a little batch script that I could use to clear out the user’s temporary files folder and the Windows temporary file folder when they log in.  I’ll just have to make a note to myself to do it on a regular basis.   The first script I put together was pretty simple and worked, kind of.  The only problem was that it didn’t delete the subfolders in either temp folder, it just deleted all the files.  That’s fine because it solves the main problem, but I don’t like having those empty folders there either.  I couldn’t find a switch that said to delete subfolders too, just one to delete the entire folder – which wasn’t what I wanted.

I found a post in the SpiceWorks forum that had a script somebody threw together that deletes all the files and the empty subfolders.  I definitely do not understand all the switches and variables that are in it, but I’ve tested and it works perfectly.

First script: Deletes only the files in the temp folders and the files within the subfolders.

rem clean up user’s temp folder
cd %tmp%
del /q /f /s *.*

Rem clean up windows temp folder
cd %systemroot%\temp
del /q /f /s *.*

Second script: deletes the files and empty subfolders.

echo off

REM clean up user’s temp folder
set CAT=%tmp%

dir “%%CAT%%”/s/b/a | sort /r >> %TEMP%\files2del.txt
for /f “delims=;” %%D in (%TEMP%\files2del.txt) do (del /q “%%D” & rd “%%D”)
del /q %TEMP%\files2del.txt

REM clean up windows temp folder
set CAT=%systemroot%\temp

dir “%%CAT%%”/s/b/a | sort /r >> %TEMP%\files2del.txt
for /f “delims=;” %%D in (%TEMP%\files2del.txt) do (del /q “%%D” & rd “%%D”)
del /q %TEMP%\files2del.txt

To change the folders these scripts are cleaning out you want to change the “cd” lines in the first one and the parts after “CAT=” in the second one.


There are some issues with this script that could potentially cause problems. I haven’t experienced any problems with it, but others have. Please see the comments below. I’ll update the post when I have a chance to actually work on it.

  1. #1 by Jonathan Roberts on October 23, 2014 - 2:39 PM

    Here is a more compact batch file for deleting folders and files in the %TEMP% folder
    (Explanation of script can be seen using ‘help FOR’ at cmd prompt):

    REM clean up user Temp folder

    CD %TEMP%

    FOR /R %TEMP% %%F IN (*) DO DEL /q/s/f “%%F”


    FOR /D %%D IN (*) DO RMDIR /s/q “%%D”


  2. #2 by Anonymous on August 8, 2012 - 2:49 AM

    Do not run the Command! it doesn’t delete temp files it delete’s the entire home drive!

    • #3 by jen3ral on August 10, 2012 - 9:02 AM

      Sorry, another person commented on a better option down below. I guess I’m just lucky, I’ve never had a problem with it. I’ll play around with it when I have a chance and update my post.

  3. #4 by Jason Mitcheson on July 11, 2012 - 8:25 PM

    Haha, your script is dangerous.. it doesn’t check to see if the CD command was successful before executing the delete. CD doesn’t work if you’re changing drives. If you have your temp folder on a different than the command prompt, the command executes successfully, but doesn’t actually change directories, so the script will delete everything from within the current location of the command prompt.

    Far better is del /q /f /s %temp%\*

    • #5 by jen3ral on July 11, 2012 - 8:40 PM

      I guess I’m lucky I never had a problem with it. Thanks. I’ll change what I use.

  4. #6 by Marlon on February 10, 2012 - 8:25 AM

    CCleaner is great but when I’m at work I can’t install that on every computer, there are license infringements..therefore, batch is the way to go. it’s better than manually tracking these places 😉

    • #7 by jen3ral on February 10, 2012 - 4:59 PM

      Just going to the server and changing people’s login script to include this is so much easier to manage.

      This only empties out the windows temp folder and the specific user’s temp folder that is logging in. So if 4 people use one workstation then the script will have to be assigned to each person to clear out the temp files in each of their profiles.

  5. #8 by Marlon on February 10, 2012 - 8:05 AM

    Or I should say “all user profiles”

  6. #9 by Marlon on February 10, 2012 - 8:03 AM

    Does this clean the temp files in the user profiles too?

  7. #10 by nhoelpitan on December 20, 2011 - 5:54 PM

    can i delete all the files and folders inside the temp folder.

    • #11 by jen3ral on December 21, 2011 - 9:42 AM

      Yes. It should be cleaned out every so often. I find it strange that Windows’ own cleanup utility doesn’t do the job. I use Ccleaner on my personal machines probably once a month.

  8. #12 by Andrew Phelps on June 13, 2010 - 8:14 AM

    There are a lot of really clever people on the Spiceworks community and some REALLY useful scripts, i love the outlook signature maker one.

    • #13 by jen3ral on June 13, 2010 - 11:09 PM

      I had never come across SpiceWorks until I stumbled upon this script. I’ll definitely be keeping it in my favorites.

  1. Why Cleaning Up After Windows Can Make Your Slow Computer Faster |

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: