Index: libgnomeui/gnome-thumbnail.c
===================================================================
--- libgnomeui/gnome-thumbnail.c	(revision 5587)
+++ libgnomeui/gnome-thumbnail.c	(working copy)
@@ -1421,3 +1421,181 @@
   
   return TRUE;
 }
+
+
+typedef struct {
+  time_t now;
+  time_t max_age;
+  glong  total_size;
+  glong  max_size;
+} PurgeData;
+
+
+typedef struct {
+  time_t  mtime;
+  char   *path;
+  glong   size;
+} ThumbData;  
+
+
+static void
+thumb_data_free (gpointer data)
+{
+  ThumbData *info = data;
+
+  if (info)
+    {
+      g_free (info->path);
+      g_free (info);
+    }
+}
+
+
+static GList *
+read_dir_for_purge (const char *path, GList *files)
+{
+  DIR *dir;
+  struct dirent *dirent;
+
+  dir = opendir (path);
+
+  if (dir)
+    {
+      while ((dirent = readdir (dir)) != NULL)
+        {
+          if (strlen (dirent->d_name) == 36 &&
+              strcmp (dirent->d_name + 32, ".png") == 0)
+            {
+              struct stat statbuf;
+	      char       *full_path;
+
+              full_path = g_build_filename (path, dirent->d_name, NULL);
+              if (g_stat (full_path, &statbuf) == 0) {
+                ThumbData *info;
+                info = g_new0 (ThumbData, 1);
+                info->path = g_strdup (full_path);
+                info->mtime = statbuf.st_mtime;
+		info->size = statbuf.st_size;
+		files = g_list_prepend (files, info);
+              }	
+	      g_free (full_path);
+            }
+        }
+      closedir (dir);
+    }
+
+  return files;
+}
+
+
+static void
+purge_old_thumbnails (ThumbData *info, PurgeData *purge_data)
+{
+  if ((purge_data->now - info->mtime) > purge_data->max_age) {
+    info->size = 0;
+    g_unlink (info->path);
+  }
+  else {
+    purge_data->total_size += info->size;
+  }
+}
+
+
+static int 
+sort_file_mtime (ThumbData *file1, ThumbData *file2)
+{
+  if (file1->mtime < file2->mtime) return -1;
+  if (file1->mtime > file2->mtime) return 1;
+  return 0;
+}
+
+
+static int
+get_gconf_int_with_nonzero_default (char *key, int default_value)
+{
+  GConfValue  *value;
+  GConfClient *client;
+  int          res;
+
+  client = gconf_client_get_default ();
+  value = gconf_client_get (client, key, NULL);
+
+  if (value == NULL) { 
+    res = default_value;
+    gconf_client_set_int (client, key, default_value, NULL);
+  }
+  else {
+    res = gconf_value_get_int (value);
+    gconf_value_free (value);
+  }
+
+  g_object_unref (client);
+
+  return res;
+}	
+
+
+/**
+ * gnome_thumbnail_purge_cache:
+ *
+ * Prunes the cache, eliminating thumbnails that are older than
+ * a threshold in days set by the /desktop/gnome/thumbnail_cache/max_age 
+ * gconf key. After that, if the cache size exceeds a threshold in megabytes
+ * set by the /desktop/gnome/thumbnail_cache/max_size gconf key, the oldest 
+ * remaining files will be deleted until the cache size is reduced below
+ * this threshold.
+ *
+ * Return value: None.
+ *
+ * Since: 2.22.1
+ **/
+void
+gnome_thumbnail_purge_cache (void)
+{
+  char      *path;
+  GList     *files = NULL;
+  PurgeData *purge_data;
+
+  path = g_build_filename (g_get_home_dir (),
+                           ".thumbnails",
+                           "normal",
+                           NULL);
+  files = read_dir_for_purge (path, files);
+  g_free (path);
+
+  path = g_build_filename (g_get_home_dir (),
+                           ".thumbnails",
+                           "large",
+                           NULL);
+  files = read_dir_for_purge (path, files);
+  g_free (path);
+
+  path = g_build_filename (g_get_home_dir (),
+                           ".thumbnails",
+                           "fail",
+                           NULL);
+  files = read_dir_for_purge (path, files);
+  g_free (path);
+
+  purge_data = g_new0 (PurgeData, 1);
+  time (&purge_data->now);
+  purge_data->max_age = get_gconf_int_with_nonzero_default ("/desktop/gnome/thumbnail_cache/max_age", 90) * 24 * 60 * 60;
+  purge_data->max_size = get_gconf_int_with_nonzero_default ("/desktop/gnome/thumbnail_cache/max_size", 256) * 1024 * 1024;
+
+  if (purge_data->max_age >= 0)
+    g_list_foreach (files, (GFunc) purge_old_thumbnails, purge_data);
+
+  if ((purge_data->total_size > purge_data->max_size) && (purge_data->max_size >= 0)) {
+    GList *scan = NULL;
+    files = g_list_sort (files, (GCompareFunc) sort_file_mtime);
+    for (scan = files; scan && (purge_data->total_size > purge_data->max_size); scan = scan->next) {
+      ThumbData *info = scan->data;
+      g_unlink (info->path);
+      purge_data->total_size -= info->size;
+    }
+  }
+    
+  g_list_foreach (files, (GFunc) thumb_data_free, NULL);
+  g_list_free (files);
+  g_free (purge_data);
+}
Index: libgnomeui/gnome-thumbnail.h
===================================================================
--- libgnomeui/gnome-thumbnail.h	(revision 5587)
+++ libgnomeui/gnome-thumbnail.h	(working copy)
@@ -93,8 +93,8 @@
 char *     gnome_thumbnail_md5               (const char         *uri);
 char *     gnome_thumbnail_path_for_uri      (const char         *uri,
 					      GnomeThumbnailSize  size);
+void       gnome_thumbnail_purge_cache       (void);
 
-
 /* Pixbuf utils */
 
 GdkPixbuf *gnome_thumbnail_scale_down_pixbuf (GdkPixbuf          *pixbuf,
