How to Clear Temp Folder
💡 Clearing the temporary folder is a simple yet effective way to free up disk space and maintain optimal performance. This guide walks you through the steps to automate the process for consistent and hassle-free maintenance.
Problem Description
Temporary files accumulate over time in the system’s temp folder, consuming valuable disk space and potentially slowing down performance. These files are typically generated by the automation processes during runtime and may no longer serve any purpose. Regularly clearing the temp folder ensures efficient use of storage and prevents unnecessary resource consumption. In turn, this will help to eliminate another potential source of error for your RPA processes.
Solution
Follow these steps to create a reusable workflow for clearing the temp folder:
Step 1: Create a New Workflow
Create a new Sequence workflow and renamed it as ‘ReusableClearTempFolder.xaml’.
Step 2: Add an "Invoke Code" Activity
Drag and drop an Invoke Code activity into your workflow.
Set the Language property to VB.Net.
Enter the following code snippet:
Dim tempFolderPath As String = Path.GetTempPath()
Try
Dim tempEntries() As String = Directory.GetFileSystemEntries(tempFolderPath)
Console.WriteLine("Total temp files : " & tempEntries.Count.ToString)
For Each entry As String In tempEntries
Try
If File.Exists(entry) Then
File.Delete(entry)
Console.WriteLine("File deleted: " & entry)
ElseIf Directory.Exists(entry) Then
Directory.Delete(entry, True) ' Deleting a folder recursively
Console.WriteLine("Folder deleted: " & entry)
End If
Catch ex As Exception
Console.WriteLine("Failed to delete: " & entry & " - " & ex.Message)
End Try
Next
Catch ex As Exception
Console.WriteLine("An error occurred: " & ex.Message)
End Try
Explanation of the Code
Path.GetTempPath(): Retrieves the path of the system's temp folder.
Directory.GetFileSystemEntries(): Lists all files and subdirectories in the temp folder.
File.Delete(): Deletes individual files while handling potential exceptions.
Directory.Delete(entry, True): Deletes directories and their contents recursively.
Try-Catch: Ensures that errors during deletion are handled gracefully and logged.
Step 3: Test and Validate
Integrate the workflow into your automation process and verify that the unnecessary files and folders are being cleared effectively. For example:
Additional Information
Last Updated: 2 Jan 2025
Tested Version(s): Studio 22.10.4.0
Prerequisites: None
Dependencies: None
Known Issues: None
References
Nil