How to clear edge browser cache, cookies and history using uipath

I want to clear edge browser cache, cookies and history using UiPath.
Please share the steps for this using UiPath.
@ashokkarale -Requesting your suggestion on this.

@Sagar_Shinde3

Welcome to the community

An easier and straight way would be to use the incognito mode which doesnot save all of these at all ..so no clearing

But if you dont want to and still clear please check this

Cheers

1 Like

@Sagar_Shinde3

You can use this code in Invoke code:

' --- 1) Stop Edge & WebView2 (avoid file locks) ---
For Each pname As String In New String() {"msedge", "msedgewebview2"}
    For Each p As System.Diagnostics.Process In System.Diagnostics.Process.GetProcessesByName(pname)
        Try
            p.Kill()
            p.WaitForExit(5000)
        Catch
        End Try
    Next
Next

' --- 2) Resolve User Data root ---
Dim userData As String = Path.Combine(
    Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
    "Microsoft\Edge\User Data"
)

Dim sb As New System.Text.StringBuilder()
sb.AppendLine("Edge Cleanup Report")
sb.AppendLine("User Data root: " & userData)
sb.AppendLine("AlsoClearCookies: " & AlsoClearCookies.ToString())
sb.AppendLine(New String("-"c, 60))

If Not Directory.Exists(userData) Then
    sb.AppendLine("Edge 'User Data' path not found. Nothing to clean.")
    Report = sb.ToString()
    Return
End If

' --- 3) Gather profile directories as STRING paths ---
Dim profileDirs As String() = New String() {}
Try
    profileDirs = Directory.GetDirectories(userData)  ' e.g., Default, Profile 1, etc.
Catch
    profileDirs = New String() {}
End Try

' --- 4) Targets: Cache folders ---
Dim cacheTargets As String() = {
    Path.Combine("Cache", "Cache_Data"),
    "Code Cache",
    "GPUCache",
    "DawnCache",
    "ShaderCache",
    "GrShaderCache",
    "GraphiteDawnCache"
}

' --- 5) Targets: History files/folders (relative to profile root) ---
Dim historyFiles As String() = {
    "History",
    "History-journal",
    "Visited Links",
    "Top Sites",
    "Shortcuts"
}
Dim historyFolders As String() = {
    "Sessions"
}

Dim profilesTouched As Integer = 0

For Each profileDir As String In profileDirs
    Dim profileName As String = Path.GetFileName(profileDir)
    Dim touched As Boolean = False

    sb.AppendLine("Profile: " & profileName)

    ' ---- Cache cleanup ----
    For Each rel As String In cacheTargets
        Dim full As String = Path.Combine(profileDir, rel)
        If Directory.Exists(full) Then
            Try
                ' Normalize file attributes to avoid readonly issues
                For Each f As String In Directory.EnumerateFiles(full, "*", SearchOption.AllDirectories)
                    Try : File.SetAttributes(f, FileAttributes.Normal) : Catch : End Try
                Next
                Directory.Delete(full, True)
                sb.AppendLine("  Cleared (cache): " & rel)
                touched = True
            Catch ex As UnauthorizedAccessException
                sb.AppendLine("  Skipped (cache, access denied): " & rel)
            Catch ex As IOException
                sb.AppendLine("  Skipped (cache, in use/IO): " & rel)
            End Try
        Else
            sb.AppendLine("  Not found (cache): " & rel)
        End If
    Next

    ' ---- History cleanup ----
    For Each fRel As String In historyFiles
        Dim fPath As String = Path.Combine(profileDir, fRel)
        If File.Exists(fPath) Then
            Try
                File.SetAttributes(fPath, FileAttributes.Normal)
            Catch
            End Try
            Try
                File.Delete(fPath)
                sb.AppendLine("  Cleared (history file): " & fRel)
                touched = True
            Catch ex As UnauthorizedAccessException
                sb.AppendLine("  Skipped (history file, access denied): " & fRel)
            Catch ex As IOException
                sb.AppendLine("  Skipped (history file, in use/IO): " & fRel)
            End Try
        Else
            sb.AppendLine("  Not found (history file): " & fRel)
        End If
    Next

    For Each dRel As String In historyFolders
        Dim dPath As String = Path.Combine(profileDir, dRel)
        If Directory.Exists(dPath) Then
            Try
                ' Normalize attributes inside
                For Each f As String In Directory.EnumerateFiles(dPath, "*", SearchOption.AllDirectories)
                    Try : File.SetAttributes(f, FileAttributes.Normal) : Catch : End Try
                Next
                Directory.Delete(dPath, True)
                sb.AppendLine("  Cleared (history folder): " & dRel)
                touched = True
            Catch ex As UnauthorizedAccessException
                sb.AppendLine("  Skipped (history folder, access denied): " & dRel)
            Catch ex As IOException
                sb.AppendLine("  Skipped (history folder, in use/IO): " & dRel)
            End Try
        Else
            sb.AppendLine("  Not found (history folder): " & dRel)
        End If
    Next

    ' ---- Cookies (optional) ----
    If AlsoClearCookies Then
        Dim cookiesPath As String = Path.Combine(profileDir, Path.Combine("Network", "Cookies"))
        If File.Exists(cookiesPath) Then
            Try : File.SetAttributes(cookiesPath, FileAttributes.Normal) : Catch : End Try
            Try
                File.Delete(cookiesPath)
                sb.AppendLine("  Cleared: Network\Cookies (SQLite)")
                touched = True
            Catch ex As UnauthorizedAccessException
                sb.AppendLine("  Skipped Cookies (access denied)")
            Catch ex As IOException
                sb.AppendLine("  Skipped Cookies (in use/IO)")
            End Try
        Else
            sb.AppendLine("  Cookies DB not found")
        End If
    End If

    If touched Then profilesTouched += 1
    sb.AppendLine()
Next

' --- 6) Small wait (optional) ---
System.Threading.Thread.Sleep(1000)

sb.AppendLine(New String("-"c, 60))
sb.AppendLine("Profiles touched: " & profilesTouched.ToString() & " / " & profileDirs.Length.ToString())
sb.AppendLine("Done.")

Report = sb.ToString()

These argument to control if need to clear cache and get the logs of cache cleanup.

incognito would be my goto option but some companies might have restriction on that so here’s the alternative :

Settings > Privacy and security > Site Settings > Look for On-device site data, under this select this option:

OR

you can customize the setting for site/sites to not to store any data on your device, by below :

i do use these settings and once they are done you are good to go..
only thing is you’ll have to set these settings(one time) on all machines that you use.(which should be fine).

if this helped, pls mark it as solution.

SG.

1 Like

Just realized its on edge,
so here it is :slight_smile:


just configure these and you are good to go.

SG.

1 Like

Hi @Anil_G , If I use the modern activities then I can directly open browser with Incognito mode but here I am using classic activities for launching the browser.
Any other way?

1 Like

@Sagar_Shinde3

Even in classic you have incognito mode

also I have given other way as well

cheers

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.