4

I try to clear the firefox 8 browser cache using programmatically. I am developing as a site using asp.net, I need to clear the browser cache for the security reason. I tried many ways to clear the cache but none seems to work. Any ideas?

Cœur
  • 37,241
  • 25
  • 195
  • 267
user1070716
  • 89
  • 1
  • 1
  • 5

6 Answers6

11

Yes you can do it But........

You can't clear a browser's history via code because of browsers security reasons.

But you can delete all the files and folders under browsers "cache" directory using file operation.

eg. Mozilla's default cache location(hidden) is "..AppData\Local\Mozilla\Firefox\Profiles\2nfq77n2.default\Cache"

How to delete all files and folders in a directory? try it!

Community
  • 1
  • 1
Tokendra Kumar Sahu
  • 3,524
  • 11
  • 28
  • 29
  • can't delete this directory,because using another process or program.how to solve it.. – user1070716 Nov 29 '11 at 14:26
  • ok you can,t delete it, but you can clear the cache by deleting subdir of cache. – Tokendra Kumar Sahu Nov 30 '11 at 12:34
  • deleting subdirectory of cache is fine.but cache is not clear. Note :once firefox is closed all the files are deleted,otherwise i can't delete it......Any idea? – user1070716 Dec 01 '11 at 08:51
  • You mean when Firefox is running , it will not allow to delete sub-directories, But I tried it and it will even works fine while Firefox is running. May be your requirement is someway different. – Tokendra Kumar Sahu Dec 02 '11 at 05:34
8

I don't think this would be possible due to security reasons . At max you can set HTTP header to tell the browser not to chache your pages like this :

Cache-Control: no-cache
Sandeep Pathak
  • 10,567
  • 8
  • 45
  • 57
3

It is not possible to clear browser's cache programmatically, however you can stop caching from your application.

Below code will help you for disabling caching and clears existing cache from your application:

public static void DisablePageCaching()
{
    //Used for disabling page caching
    HttpContext.Current.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
    HttpContext.Current.Response.Cache.SetValidUntilExpires(false);
    HttpContext.Current.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
    HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
    HttpContext.Current.Response.Cache.SetNoStore();
} 
Elias Hossain
  • 4,410
  • 1
  • 19
  • 33
1

Use this code (C#):

public static void DeleteFirefoxCache()
    {
        string profilesPath = @"Mozilla\Firefox\Profiles";
        string localProfiles = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), profilesPath);
        string roamingProfiles = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), profilesPath);

        if (Directory.Exists(localProfiles))
        {
            var profiles = Directory.GetDirectories(localProfiles).OfType<string>().ToList();
            profiles.RemoveAll(prfl => prfl.ToLowerInvariant().EndsWith("geolocation")); // do not delete this profile.

            profiles.ForEach(delegate(string path) 
            {                    
                var files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).ToList<string>();

                foreach (string file in files)
                {
                    if (!Common.IsFileLocked(new FileInfo(file)))
                        File.Delete(file);
                }            
            });
        }

        if (Directory.Exists(roamingProfiles))
        {
            var profiles = Directory.GetDirectories(roamingProfiles).OfType<string>().ToList();

            profiles.RemoveAll(prfl => prfl.ToLowerInvariant().EndsWith("geolocation")); // do not delete this profile.
            profiles.ForEach(delegate(string path)
            {
                var dirs = Directory.GetDirectories(path, "*", SearchOption.AllDirectories).OfType<string>().ToList();
                dirs.ForEach(delegate(string dir) 
                {
                    var files = Directory.GetFiles(dir, "*.*", SearchOption.AllDirectories).ToList<string>();

                    foreach (string file in files)
                    {
                        if (!Common.IsFileLocked(new FileInfo(file)))
                            File.Delete(file);
                    }   
                });

                var files0 = Directory.GetFiles(path, "*", SearchOption.TopDirectoryOnly).OfType<string>().ToList();
                files0.ForEach(delegate(string file) 
                {
                    if (!Common.IsFileLocked(new FileInfo(file)))
                        File.Delete(file);
                });
            });
        }                    
    }
1

My solution:

string UserProfile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
            try
            {
                string id = string.Empty;
                var lines = File.ReadAllLines($@"{UserProfile}\AppData\Roaming\Mozilla\Firefox\profiles.ini");
                foreach (var line in lines)
                {
                    if (line.Contains("Path=Profiles/"))
                    {
                        var text = line.Replace("Path=Profiles/", "");
                        id = text.Trim();
                    }
                }
                Array.ForEach(Directory.GetFiles($@"{UserProfile}\AppData\Local\Mozilla\Firefox\Profiles\{id}\cache2\entries"), File.Delete);
            }
            catch { }
-1

In asp.net/ c# you can trigger this.

string cacheKey = "TestCache";

//Add cache
   Cache.Add(cacheKey, "Cache content", null, DateTime.Now.AddMinutes(30), 
TimeSpan.Zero, CacheItemPriority.High, null);

Cache.Remove(cacheKey); //C# clear cache