Unused files (images) in pages

My clients are uploading 100+ files (images) to a single page and only use 10 of them. Can I check in a way if a file is used in a blueprint field so I can remove the others that are not used?

What does that mean? You have a files field where they select files out of all the uploaded files and only those are ever shown on the page?

Then yes, you can get the values of that field and remove the rest.

Hi @pixelijn,

I have a files fields. For example to upload/select a header image. But after they uploaded one and want another picture they click the β€œminus” icon that only removes it from the field, but not from the actual files in the page. Does that make sense?

So I would like to find an option to check image that are not used in a field and remove the rest.

Yes, for each individual page you could get all the files from a field

$selectedFiles = $page->somefield()->toFile(); // or `toFiles()` for multiple files

$filesToRemove = $page->files()->not($selectedFiles);

Then loop through $filesToRemove to remove them: $file->delete() | Kirby CMS

However, this would only work if you have only one field where a file is used and you would have to get all used files from all fields first (cover, in blocks or textareas) before you can start deleting files.

@texnixe This could be a handy addition to your cleanup script.

1 Like