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.

eZ debug

Timing: Jan 18 2025 10:26:10
Script start
Timing: Jan 18 2025 10:26:10
Module start 'layout'
Timing: Jan 18 2025 10:26:10
Module start 'content'
Timing: Jan 18 2025 10:26:11
Module end 'content'
Timing: Jan 18 2025 10:26:11
Script end

Main resources:

Total runtime0.9721 sec
Peak memory usage4,096.0000 KB
Database Queries69

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0051 588.0469152.6406
Module start 'layout' 0.00510.0025 740.687539.4844
Module start 'content' 0.00750.9631 780.1719596.3047
Module end 'content' 0.97060.0015 1,376.476620.1250
Script end 0.9721  1,396.6016 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00310.3149160.0002
Check MTime0.00130.1308160.0001
Mysql Total
Database connection0.00090.097610.0009
Mysqli_queries0.915294.1432690.0133
Looping result0.00060.0654670.0000
Template Total0.945897.320.4729
Template load0.00180.188720.0009
Template processing0.943997.102120.4720
Template load and register function0.00020.022910.0002
states
state_id_array0.00070.073510.0007
state_identifier_array0.00110.114720.0006
Override
Cache load0.00150.1588380.0000
Sytem overhead
Fetch class attribute can translate value0.00100.105220.0005
Fetch class attribute name0.00240.244780.0003
XML
Image XML parsing0.00070.073620.0004
class_abstraction
Instantiating content class attribute0.00000.0019100.0000
General
dbfile0.00100.1018170.0001
String conversion0.00000.000440.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
7content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
3content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
9content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
3content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
2content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 26
 Number of unique templates used: 7

Time used to render debug report: 0.0001 secs