How to delete old image files in storage dir

Author Message

Martin Jerie

Wednesday 23 March 2011 12:05:31 am

I import every week about 3000 product.
In storage dir (var/mysite/storage/images) remains old (unused) versions and directory unnecessarily increasing.

Is there any way how to delete these files?

Thanks
Martin

Steven E. Bailey

Wednesday 23 March 2011 1:45:58 am

What version of ezpublish are you using?

Do you have access to the command line? Are you on a linux or windows machine?

What exactly do you want to delete: the images of previous versions that are archived, images that are still in the database but are older than some time period, or just the image aliases of an image that is still in the database but is older than some time period, or what? Meaning, as long as you have access to the command line (on a linux machine) there should be easy solutions for all of the above but the solution depends on exactly what it is that you want to delete.

I also thought there was a script in the update directory that would remove images that were not in the imagefile table... but I can't seem to find it... I do have a script that I think I might have written myself.

Certified eZPublish developer
http://ez.no/certification/verify/396111

Available for ezpublish troubleshooting, hosting and custom extension development: http://www.leidentech.com

Martin Jerie

Wednesday 23 March 2011 3:03:01 am

I use version 4.1.3 on linux and i have access to command line.

To import products i use script where i also update image attribute.

if ($contentClassAttribute->attribute("identifier") == "image") $contentObjectAttribute->fromString('/var/www/petek/var/petek/erba.jpg');

In content.ini -> DefaultVersionHistoryLimit i have value 5. So i have only 5 versions of the product.
But in storage directory for these product var/mysite/storage/images ... i have now 35 directories (versions) of the image attribute.

But i need only last 5 versions ...

Thanks

Steven E. Bailey

Wednesday 23 March 2011 8:40:30 am

Here's the script I use -try it out with the --simulate option first to see if will delete what you want - no warranty, not responsible if it deletes everything, use at your own risk etc. etc. This has to be based on someone elses code because I wouldn't have put any cluster support in there. Also make sure the use you run this as has the right permissions to delete the files, otherwise it will silently fail.

 <?php
require 'autoload.php';
$cli = eZCLI::instance();
$script = eZScript::instance( array( 'description' => (
          "Removes images not found in the ezimagefile table" ),
                                     'use-session' => false,
                                     'use-modules' => true,
                                     'use-extensions' => true ) );
$script->startup();

$options = $script->getOptions( "[simulate]", "",  array( 'simulate' => "simulate, doesn't delete") );

$script->initialize();

eZDebug::writeDebug( $options, "options" );

$clusterdelcount = 0;
$total = 0;
$nodelcount = 0;
$rmdircount = 0;
$totaldir = 0;
$simulate = $options["simulate"];
eZDebug::writeDebug( $simulate, "SIMULATE" );

$sys = eZSys::instance();
$storage_dir = $sys->storageDirectory()."/images";
eZDebug::writeDebug( $storage_dir, "STORAGE DIR" );

$dir_iterator = new RecursiveDirectoryIterator($storage_dir);
$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::CHILD_FIRST);

foreach ($iterator as $filename => $cur) {
        if ( $cur->isFile() ) {
                $imageFiles = eZImageFile::fetchByFilePath( false, $filename, false );
                if ($imageFiles == NULL) {
                        $ezfile = eZClusterFileHandler::instance( $filename );
                        if ( $ezfile->exists() )
                        {
                                if (!$simulate) $ezfile->delete();
                                echo "Deleting: ".$filename."\n";
                                $clusterdelcount++;
                        }
                } else {
                        $nodelcount++;
                }
        $total++;
        } elseif ( $cur->isDir() ) {
                if ( count(scandir($filename)) == 2 ) {
                        if (!$simulate) rmdir($filename);
                        echo $filename." is an empty dir - REMOVED\n";
                        $rmdircount++;
                }
        $totaldir++;
        }
}
echo "Deleted ".$clusterdelcount." out of ".$total." files\n";
echo "Leaving ".$nodelcount." files behind\n";
echo "Also deleted ".$rmdircount." empty directories out of ".$totaldir." total\n";
    $script->shutdown();
?>

Certified eZPublish developer
http://ez.no/certification/verify/396111

Available for ezpublish troubleshooting, hosting and custom extension development: http://www.leidentech.com

Martin Jerie

Thursday 24 March 2011 12:59:28 am

Thanks for the script.

Yes it works - but only delete images from deleted nodes ...

But i still have problem with 35 versions of the image attribute - because they are all in ezimagefile table and this script doesn´t delete them (old 30 versions).

So my question is how to delete these files which are not used (in my case in last 5 versions of the product)

Steven E. Bailey

Thursday 24 March 2011 5:43:38 am

If the archive of the version for that image has been removed (whether manually or automatically) then the image should have been removed from the ezimagefile table too. I'm not sure what's going on... are you sure you are only saving the 5 versions? The relevant ini setting is in content.ini [VersionManagement] DefaultVersionHistoryLimit.

I remember there was an issue at some point - http://issues.ez.no/IssueView.php?Id=15155 - that appears to be your problem since it was fixed in 4.1.4

Certified eZPublish developer
http://ez.no/certification/verify/396111

Available for ezpublish troubleshooting, hosting and custom extension development: http://www.leidentech.com

Martin Jerie

Thursday 24 March 2011 7:49:44 am

Yes it was this issue in 4.1.3, after upgrade is everything OK

Thank you again.

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.