Last Updated: May 11, 2020
·
1.274K
· paulhenrich

Find unused images in your app

Have an app with unused images you want to clean up? If you've dynamically composed image names in your application code, you won't be able to scan for unused images with a simple git grep. Instead, look at the atime on your images. Note the time, exercise your application, and then look at the atimes of files. Files that haven't their atimes updated weren't accessed in the course of your test. Review them and see if you can safely delete them.

Here's how I did this on a rails app recently:

Note the time &/ ls -ult app/assets/images. Wait a minute.

Crawl your site and exercise logged-in functionality with wget and your browser tests respectively.

wget -r -k -p -np -e robots=off http://localhost:5000/
rake spec:features

Check the atimes of your images again: ls -ult app/assets/images

At the end of this process I used the process suggested in this pivotal blog post, looping over the end result with git grep to double check that none of my candidates for deletion are referenced directly, as a sanity check.

3 Responses
Add your response

Do make sure that the filesystem is not mounted with the 'noatime' option. It's a common thing to do on networked filesystems (to cut down on network usage) and SSD devices (to cut down on writes). Some sysadmins/devops may use it to just squeeze every last drop of file I/O performance from the system.

over 1 year ago ·

Thanks @omouse. @charstring great point, thanks.

over 1 year ago ·

It's also worth noting that wget won't exercise any javascript, so if you have a javascript app, you will have to find another way to crawl/exercise it fully.

over 1 year ago ·