@@ -17,6 +17,8 @@ class RemoveUnusedMediaCommand extends Command
17
17
{
18
18
private const OPTION_DRY_RUN = 'dry-run ' ;
19
19
private const OPTION_INCLUDING_CACHE = 'including-cache ' ;
20
+ private const OPTION_ONLY_CACHE = 'only-cache ' ;
21
+ private const OPTION_INCLUDING_RELATION_ENTITY = 'including-relation ' ;
20
22
private const OPTION_FORCE = 'force ' ;
21
23
private const COMMAND_NAME_EAV_MEDIA_REMOVE_UNUSED = 'eav:media:remove-unused ' ;
22
24
@@ -44,6 +46,11 @@ public function execute(InputInterface $input, OutputInterface $output): int
44
46
$ isForce = $ input ->getOption (self ::OPTION_FORCE );
45
47
$ isDryRun = $ input ->getOption (self ::OPTION_DRY_RUN );
46
48
$ deleteCacheAsWell = $ input ->getOption (self ::OPTION_INCLUDING_CACHE );
49
+ $ deleteOnlyCache = $ input ->getOption (self ::OPTION_ONLY_CACHE );
50
+ if ($ deleteOnlyCache ) {
51
+ $ deleteCacheAsWell =true ;
52
+ }
53
+ $ deleteNotInRelation = $ input ->getOption (self ::OPTION_INCLUDING_RELATION_ENTITY );
47
54
48
55
if (!$ isDryRun && !$ isForce ) {
49
56
if (!$ input ->isInteractive ()) {
@@ -76,6 +83,12 @@ public function execute(InputInterface $input, OutputInterface $output): int
76
83
77
84
$ imagesToKeep = $ connection ->fetchCol ('SELECT value FROM ' . $ mediaGalleryTable );
78
85
86
+ if ($ deleteNotInRelation ) {
87
+ $ mediaGalleryToEntityTable = $ this ->resourceConnection ->getTableName ('catalog_product_entity_media_gallery_value_to_entity ' );
88
+ $ sql ='SELECT value FROM ' . $ mediaGalleryTable . ' where value_id IN (SELECT value_id from ' .$ mediaGalleryToEntityTable .') ' ;
89
+ $ imagesToKeep = $ connection ->fetchCol ($ sql );
90
+ }
91
+
79
92
foreach (new RecursiveIteratorIterator ($ directoryIterator ) as $ file ) {
80
93
// Directory guard
81
94
if (is_dir ($ file )) {
@@ -87,6 +100,11 @@ public function execute(InputInterface $input, OutputInterface $output): int
87
100
continue ;
88
101
}
89
102
103
+ // Original image guard if option --only-cache
104
+ if (!$ this ->isInCachePath ($ file ) && $ deleteOnlyCache ) {
105
+ continue ;
106
+ }
107
+
90
108
$ filePath = str_replace ($ imageDir , "" , $ file );
91
109
// Filepath guard
92
110
if (empty ($ filePath )) {
@@ -157,13 +175,24 @@ protected function configure(): void
157
175
{
158
176
$ this ->setName (self ::COMMAND_NAME_EAV_MEDIA_REMOVE_UNUSED );
159
177
$ this ->setDescription ('Remove unused product images ' );
160
-
161
178
$ this ->addOption (
162
179
self ::OPTION_INCLUDING_CACHE ,
163
180
'c ' ,
164
181
null ,
165
182
'Also clear the ./cache/* entries for the corresponding images '
166
183
);
184
+ $ this ->addOption (
185
+ self ::OPTION_ONLY_CACHE ,
186
+ 'k ' ,
187
+ null ,
188
+ 'Clear only the ./cache/* entries for the corresponding images, but not the corresponding images '
189
+ );
190
+ $ this ->addOption (
191
+ self ::OPTION_INCLUDING_RELATION_ENTITY ,
192
+ 'r ' ,
193
+ null ,
194
+ 'Also clear the media not in relation table "catalog_product_entity_media_gallery_value_to_entity" '
195
+ );
167
196
$ this ->addOption (
168
197
self ::OPTION_DRY_RUN ,
169
198
'd ' ,
0 commit comments