Removing orphaned files


#1

Silverstripe Version:
4.2.1

Question:

Hi, just wondering if there’s a way to quickly removed orphaned files/images? Or run a report to tell me which ones are orphaned?


#2

You mean files that are in the assets but not in the database, or the other way around?

I think with the new FlySystem implementation, a system like the 3.x sync assets has disappeared


#3

There’s some related discussion here: No way to flush resampled images? · Issue #109 · silverstripe/silverstripe-assets · GitHub

I think you’d need to create some kind of task to build a list of all the files in the assets directory directly through PHP, then compare them with the files that SilverStripe is aware of. I’ve seen a few questions about this so the community would probably appreciate a module to bring back this functionality in SS4 :wink:


#4

I actually meant images that have no owner. I have managed to get this working using a report to show files without owners - it is however quite slow as it’s filtering by callback.

// what we want the report to return

public function sourceRecords($params = null)

{

return File::get()->exclude(['ClassName:PartialMatch'=>'Folder'])->filterByCallBack(function($item,$list){

return ($item->getOwnersCount() == 0);

        });

}

#5

Over on Issue #109 user “blueo” has added this task which takes a file ID and cleans up all of the generated image crops, resize files for one image at a time.

I installed it just now on 4.2.2 and it works perfectly. A big thanks to “blueo” for writing it.