Skip to main content

Disk cleanup- delete temp files


I am looking for a way to perform a disk cleanup that will delete temp files. I would like to perform it on command and also run daily on users computers.

Thanks in advance

14 replies

MichaelK-Automox

Could you be more specific? Are you talking about emptying the trash or are there specific directories you would want to clean out?


Forum|alt.badge.img
  • Automox Employee
  • 134 replies
  • September 16, 2021

One of our Support Engineers, Tony, put together this worklet to clean out a designated folder. Have you seen this yet?

 


  • Author
  • Novice
  • 14 replies
  • September 16, 2021

Mark,

If I wanted to delete the temp folder under c:\users\username\AppData\Local\Temp would I change $targetFolder = “Downloads” to $targetFolder = “Temp” in the Evaluation and remediation field?

image354082.png

image510826.png

image450238.png

image711730.png

image024945.png

image688867.png

image326996.png


MichaelK-Automox

$targetFolder = “Temp”
$ODtargetFolder = “AppData\Local\$targetFolder”

Making the above changes should do what you want.


  • Author
  • Novice
  • 14 replies
  • September 16, 2021

Michael,

I would like to delete files in specific directories. We want to delete the temp files in a user’s profile, temporary internet files and Windows\Temp. We have determined that some issues we have are resolved by deleting the temp files on a user’s computer and we want to automate the deletion of those files. We would also need to empty the trash folder if the deleted files are sent there.

image354082.png

image510826.png

image450238.png

image711730.png

image024945.png

image688867.png

image326996.png


  • Author
  • Novice
  • 14 replies
  • September 16, 2021

Michael,

Thank you, it did work.

image354082.png

image510826.png

image450238.png

image711730.png

image024945.png

image688867.png

image326996.png


MichaelK-Automox
# Location of folder contents to delete
$targetFolders = @(
'C:\windows\Temp'
'c:\Temp\test1'
'C:\Temp\test2'
'C:\Temp\test3'
)
$userFolders = @(
'AppData\Local\Temp'
'AppData\Local\Temporary Internet Files'
)

# Query WMI and get a list of all user profile locations
$userProfiles = (Get-WmiObject win32_userprofile).LocalPath | Where-Object { $_.Substring(0,8) -EQ "C:\Users" }

# Build a list of all possible locations
$folderList = @()
foreach ($targetFolder in $targetFolders) {
$folderList += "$targetFolder"
}
foreach ($profile in $userProfiles) {
    foreach ($userFolder in $userFolders) {
    $folderList += "$profile\$userFolder"
    }
}
# Check each location for potential files
foreach ($folder in $folderList) {
# Check each target folder for existance, then for contents    
if (Test-Path $folder -PathType Container) {
    if ((Get-ChildItem $folder -Recurse | Measure-Object).Count -gt 0) {
        # Delete contents of folder
        Get-ChildItem $folder -Recurse | ForEach { Remove-Item $_.FullName -Force -Recurse }
    }
}
}
Clear-RecycleBin -Force

I was able to throw the above together using some of the existing worklet. This should give you a little more flexibility. You can see there’s a list for user specific directories and a list for directories where you already have the absolute path. I added an empty-recyclebin to the bottom.

You’ll get some errors if you try to delete temporary internet files while they’re in use, but otherwise it seems to work.

I will try to get this reformatted in to a worklet and pushed out to the catalog within the next week. Until then, if you copy the above in to the remediation section and replace the evaluation with just “exit 1” it should work.


  • Author
  • Novice
  • 14 replies
  • September 16, 2021

Michael,

Thank you very much for this. This is exactly what I am looking for.

image354082.png

image510826.png

image450238.png

image711730.png

image024945.png

image688867.png

image326996.png


  • Author
  • Novice
  • 14 replies
  • September 16, 2021

Michael,

Just to make sure I understand correctly in the Evaluation code field I just enter exit 1 and nothing else?

image354082.png

image510826.png

image450238.png

image711730.png

image024945.png

image688867.png

image326996.png


MichaelK-Automox

Correct.


  • Author
  • Novice
  • 14 replies
  • September 16, 2021

Thanks again. I will test it on the things I want to delete.

image354082.png

image510826.png

image450238.png

image711730.png

image024945.png

image688867.png

image326996.png


  • Author
  • Novice
  • 14 replies
  • September 30, 2021

Michael,

Is there a way to add a command to delete files that are older than xx days in the downloads and OneDrive downloads?

image354082.png

image510826.png

image450238.png

image711730.png

image024945.png

image688867.png

image326996.png


MichaelK-Automox

You could use a where-object in the get-childitem function to restrict by the last write time of the file. For example, this is the same script but modified to only delete files which haven’t been written to for at least 30 days.

# Location of folder contents to delete
$targetFolders = @(
    'C:\windows\Temp'
    'c:\Temp\test1'
    'C:\Temp\test2'
    'C:\Temp\test3'
)
$userFolders = @(
    'AppData\Local\Temp'
    'AppData\Local\Temporary Internet Files'
)

# Query WMI and get a list of all user profile locations
$userProfiles = (Get-WmiObject win32_userprofile).LocalPath | Where-Object { $_.Substring(0,8) -EQ "C:\Users" }

# Build a list of all possible locations
$folderList = @()
foreach ($targetFolder in $targetFolders) {
    $folderList += "$targetFolder"
}
foreach ($profile in $userProfiles) {
    foreach ($userFolder in $userFolders) {
        $folderList += "$profile\$userFolder"
    }
}
# Check each location for potential files
foreach ($folder in $folderList) {
    # Check each target folder for existance, then for contents    
    if (Test-Path $folder -PathType Container) {
        if ((Get-ChildItem $folder -Recurse | Measure-Object).Count -gt 0) {
            # Delete contents of folder
            Get-ChildItem $folder -Recurse | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-30)} | ForEach { Remove-Item $_.FullName -Force -Recurse }
        }
    }
}
Clear-RecycleBin -Force

  • Author
  • Novice
  • 14 replies
  • October 4, 2021

Michael,

Thank you for this information. I will test it on our system.

image354082.png

image510826.png

image450238.png

image711730.png

image024945.png

image688867.png

image326996.png


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings