Script to delete temp files for all users in XP, Vista, and 7

41,730

I've used this to some success. You may need to edit it for your environment, but for me it works for XP Vista and 7. Couple of things, make sure it runs at a time with the least impact, and understand that it is as intrusive as you can really get, since it removes the folders and re creates them. You could change the rmdir to del /f and add a \ to the end of the file paths and then remove the mkdir line if you would prefer to not remove the folders and just delete the contents.

This DELETES a ton of stuff, use at your own risk.

@echo off

IF EXIST c:\windows\temp\ del /f /s /q c:\windows\temp\

DEL /f /s /q %temp%\

IF EXIST "C:\Documents and Settings\" (
    for /D %%x in ("C:\Documents and Settings\*") do ( 
        rmdir /s /q "%%x\Local Settings\Temporary Internet Files" 
        mkdir "%%x\Local Settings\Temporary Internet Files" 
    )
)

IF EXIST "C:\Documents and Settings\" (
    for /D %%x in ("C:\Documents and Settings\*") do ( 
        rmdir /s /q "%%x\Local Settings\Temp" 
        mkdir "%%x\Local Settings\Temp" 
    )
)

IF EXIST "C:\Users\" (
    for /D %%x in ("C:\Users\*") do ( 
        rmdir /s /q "%%x\AppData\Local\Temp" 
        mkdir "%%x\AppData\Local\Temp" 
    )
)

IF EXIST "C:\Users\" (
    for /D %%x in ("C:\Users\*") do ( 
        rmdir /s /q "%%x\AppData\Local\Microsoft\Windows\Temporary Internet Files" 
        mkdir "%%x\AppData\Local\Microsoft\Windows\Temporary Internet Files" 
    )
)

Note this separates out the different folders, mostly for clarity but if you wanted to condense it you could compress it to only 2 loops. An example would be:

IF EXIST "C:\Users\" (
    for /D %%x in ("C:\Users\*") do ( 
        rmdir /s /q "%%x\AppData\Local\Temp" 
        mkdir "%%x\AppData\Local\Temp" 
        rmdir /s /q "%%x\AppData\Local\Microsoft\Windows\Temporary Internet Files" 
        mkdir "%%x\AppData\Local\Microsoft\Windows\Temporary Internet Files" 
    )
)

Per request, compressed and using delete command.

@echo off

IF EXIST c:\windows\temp\ del /f /s /q c:\windows\temp\

DEL /f /s /q %temp%\

IF EXIST "C:\Users\" (
    for /D %%x in ("C:\Users\*") do ( 
        del /f /s /q "%%x\AppData\Local\Temp\" 
        del /f /s /q "%%x\AppData\Local\Microsoft\Windows\Temporary Internet Files\" 
    )
)

IF EXIST "C:\Documents and Settings\" (
    for /D %%x in ("C:\Documents and Settings\*") do ( 
        del /f /s /q "%%x\Local Settings\Temp\" 
        del /f /s /q "%%x\Local Settings\Temporary Internet Files\" 
    )
)
Share:
41,730

Related videos on Youtube

Chris
Author by

Chris

Updated on September 18, 2022

Comments

  • Chris
    Chris over 1 year

    Basically, we have numerous customers running XP and 7 with a few Vista machines.

    I've found this batch script, but it is limited to the current user (uses the %userprofile% environment variable). I'm looking for something like this, but which would run for all user accounts on the computer. The script would be run as administrator.

    For XP, it would delete the contents of:
    Local Settings\Temp
    Local Settings\Temporary Internet Files

    For Vista/7, it would delete the contents of:
    AppData\Local\Temp
    AppData\Local\Microsoft\Windows\Temporary Internet Files

    I am relatively inexperienced with scripting, and I'm not sure if a batch file can do this. Has anyone gone down this path and found a solution?

    • Split71
      Split71 over 12 years
      Are you on a domain using AD? If so you can integrate it into their logoff script to go through and clear those files using the %USERPROFILE% variable.
    • voretaq7
      voretaq7 over 12 years
      Beware when wiping out temp files -- Make sure they're old (>30 days is pretty much a standard in the Unix world) so you don't accidentally blow away something somebody needs.
    • MaskedPlant
      MaskedPlant over 12 years
      @voretaq7 Just curious but why add the Windows-7 Tag over the XP and Vista tags? OR not add all 3?
    • voretaq7
      voretaq7 over 12 years
      @MaskedPlant no particular reason - it was like 3 AM and it made sense at the time :-)
    • MaskedPlant
      MaskedPlant over 12 years
      @voretaq7 ok cool. I'm pretty new and just trying to learn from super users like you. 3AM makes plenty of sense.
    • Chris
      Chris over 12 years
      @Split71 We are using AD for most customers. Logoff script is a good idea, but it would not delete temp files remaining for users who no longer touch a particular computer. We're aiming to run this script every few months (with warning to users not to store data in temp folders).
  • Chris
    Chris over 12 years
    Awesome - this is exactly what I was looking for. I'm aware that it wipes out a lot of files. I'll try it on a few machines and report back with results.
  • Chris
    Chris over 12 years
    I will modify this to delete folder contents rather than re-create folders, because we're running the script as administrator. I don't want to create folders with improper default permissions and break things as a result.