Periodically I get an email from my hosting provider alerting me that my inode usage exceeds their resource abuse policy. Since I’m using magento on the server they recommend clearing out the contents of downloader/pearlib/cache/* and downloader/pearlib/download/* and deleting all empty .svn folders. My inode usage was still a little higher than necessary after completing these steps.

First of all, what is an inode? An inode stores basic information about a regular file, directory, or other file system object. So in a nutshell, any file, directory or file system object counts toward a point against your inode usage. Messing around I decided to clear all my caches (css, js and images). The image cache really penalized me. By clearing out the image cache my inode usage dropped from 56K to about 40K. Now, this was obviously a temporary fix as my cache is going to be rebuilt as visits come through.

One thing I did realize though was my media folder had high inode usage. Apparently when I was doing my initial product imports using the importing tools I kept on creating image files that were not really used on the site. Why keep those files around? I wrote a little script to clear those images out. Right now my inode usage has dropped to 26K and I don’t have to worry about getting suspended by my hosting provider for a little bit more time.

Before I post the script, a couple things… Test it out in a test environment before running it in production. Make a back up of your site and your image folder in case you miss something and go off deleting a bunch of random files. This is a simple script I put together to remove any unused product images in the site. I threw it up very quickly, I know it could be cleaned up quite a bit with added features and a lot of good blah blah, but I needed to bring my inode usage down quick and this script helped me clear a lot of unused images. Don’t track me down if something bad happens to your site. BACKUP! TEST! You have been warned!

Put the following two files in the /media/catalog/product/ folder:

1) Export contents of the catalog_product_entity_media_gallery table with only the value field:

2) Run the following code in the product folder. Before running the commented out code with the rm statements, see what its doing and test properly! This script will drill down into every subdirectory within the product directory looking for images and then will compare against the export file to see if the image is being used or not. If the image is not being used, the image will be deleted.

<?php
function removeUnusedImages($images,$dir) {
  $prefix = $dir . '/';
  if($handle = opendir($dir)):
    while (false !== ($file = readdir($handle))):
      if ($file === '.' || $file === '..' || $file === 'placeholder' || $file === 'cache') continue;
      $file = $prefix . $file;
      if (is_dir($file)):
        removeUnusedImages($images,$file);
      else: 
        $file = substr($file,1);
        if(!in_array($file,$images)):
          echo "Remove: {$file} \n";
          //system('ls ' . substr($file,1));
          //system('rm ' . substr($file,1));
        else: 
          echo "Dont Remove: {$file} \n";
        endif;
      endif;
    endwhile;
    closedir($handle);
  endif;
}
 
$images = file('export.txt',FILE_IGNORE_NEW_LINES);
removeUnusedImages($images,'.');
?>

Good luck!