I would like to create the list of images no longer used in any content item so they can be purged from the disk. What is the best method to generate such a list?
I'm not sure there is a best method on how to do this. The only way I can think of requires a bit of effort. That is, generating a list of all the images in the content table in the database (and potentially in modules, menus, banners, etc... Any place where images are referenced which is a lot), compare that list against all the images on the files system, and then compare between each list and remove the images which aren't used.
So this would look like:
1) Get a list of all the images in com_content's database table. Dump all the rows, regex for
<img src="images/stories/an-image.jpg" />. Another way is to use php's strip_tags() method and only allow the tag. I find this easier than writing a regex generally. Basically run a php script to build this list for you as a text document or csv.
2) Get a list of all images on the site. You could use grep from the command line to do this I believe. You could probably do it with some sort of recursive php filesystem method as well.
3) get both into a spreadsheet and run some sort of comparison against each column to see if the image is used, compiling a list of unused images in a third column.
Personally, unless you have a crap ton of images I'm not sure this is worth the effort. But you'll have to determine that pending your use case. Also there may be a far more simple solution which someone else will have to suggest!
I'm not aware of an existing extension that would do it. Your best bet would be to write a script that checks each image against tables in the database where content can be stored to see if they are in use.
If you have your access logs, you might be able to pull out all the requested image files in the last X months (you will need to play with all this data). So these are very probably in use in your actual site (yet not 100%). Depending on your content and site structure and if your site is not really huge (i.e. 1 million articles), it is very probably that the remaining are probably not in use (again not 100%).
I find this solution as easy one. Backup the images prior to deletion. You can track later on for 404 errors trying to open specific files and you may decide to restore these.
It is dirty solution, but there is really no easy way. The best option would be to follow Chad advise and to write your own parser.
Good idea, thank you. At my webhosting provider access logs are not accessible for us customers, but this answer can be still hepful for other readers.– miroxlavMay 14, 2014 at 9:14
Try to use XENU Link checker. It checks all links and marks broken links.
It has to be installed and ran on Windows.
Sadly, this is good for the exact opposite. It finds broken links (images that are missing) rather than orphaned images.– mckenzmOct 28, 2022 at 4:04
While no extensions existed when the question was asked, as of August 2018 there are at least a couple of extensions that can apparently find and delete orphaned images:
VX orphanImages (paid extension)
"VX orphanImages plugin scans your Joomla! site images folder to find items, which are not used in any content on site. It uses the core Media component to mark orphaned images in the list, so you can decide whether to keep or remove them."
R2H ImageManager (paid extension)
"R2H ImageManager allows you to move and rename images without losing the image-link in Articles and Custom HTML Modules. Simply drag & drop your images to restructure and clean-up your website. It also can list all unused images and delete them."