This day I got a notice from my web hosting provider saying that my website has used up all of its available hard disk space..... Sound familiar?.

If you have ten thousand images with several imagecache presets set for each of the images, soon or later your site will accumulate enough image file to fill out the available hard disk space.

To counter this problem actually is very easy, if you do have a root access to your webserver, you can just a cron to delete all those files periodically.

But like always, cheap shared hosting most likely won't provide you with "root access", heck they usually don't even provide you with any ssh / telnet access.

So how do we clean the file? Another way is to ftp to the site and delete it using your ftp program.... really, have you try to delete 80,000 files using ftp program?

Here comes the rescuer for this problem..... you can actually create a simple Drupal 6 module that hooks to hook_cron and execute the imagecache flush function. Let us call our little small module imagecache_cron_flush.

Now create a file named imagecache_cron_flush.module with this following lines of code :

/**
 * Implementats hook_cron()
 */
function imagecache_cron_flush_cron() {

  // lets clean the builded image on cron run to prevent full harddisk.
  // lets get the imagecache preset name for this site
  $preset = imagecache_presets($reset = FALSE);

  // Remove the preset that we want to keep from the list of preset to be flushed,
  // the preset names here is just for references, you should change them to your actual imagecache preset names
  $remove = array('100x100', '75x75', '125x85', '175x131', '600', '1800');

  // lets us loop through the preset array and determine if that preset should be flushed or not
  foreach ($preset as $key => $value) {
    if (!in_array($value['presetname'], $remove)) {
      imagecache_preset_flush($value);
    }
  }
}

Next step is to create a file named imagecache_cron_flush.info with these following lines of code :


name = Imagecache Cleaner
description = Clean imagecache files on cron 
package = ImageCache
dependencies[] = imagecache
core = 6.x

Finally, upload the files and enable it like normal Drupal 6 module and test the module by running cron.php. Hope this is helpful :)