How to find unused images in an Xcode project?

For files which are not included in project, but just hang-around in the folder, you can press

cmd ⌘ + alt ⌥ + A

and they won't be grayed out.

For files which are not referenced neither in xib nor in code, something like this might work:

#!/bin/sh
PROJ=`find . -name '*.xib' -o -name '*.[mh]'`

find . -iname '*.png' | while read png
do
    name=`basename $png`
    if ! grep -qhs "$name" "$PROJ"; then
        echo "$png is not referenced"
    fi
done

I tried Roman's solution, and I added a few tweaks to handle retina images. It works well, but remember that image names can be generated programmatically in code, and this script would incorrectly list these images as unreferenced. For example, you might have

NSString *imageName = [NSString stringWithFormat:@"image_%d.png", 1];

This script will incorrectly think image_1.png is unreferenced.

Here's the modified script:

#!/bin/sh
PROJ=`find . -name '*.xib' -o -name '*.[mh]' -o -name '*.storyboard' -o -name '*.mm'`

for png in `find . -name '*.png'`
do
   name=`basename -s .png $png`
   name=`basename -s @2x $name`
   if ! grep -qhs "$name" "$PROJ"; then
        echo "$png"
   fi
done

Please have a try LSUnusedResources.

It is heavily influenced by jeffhodnett‘s Unused, but honestly Unused is very slow, and the results are not entirely correct. So I made some performance optimization, the search speed is more faster than Unused.


This is a more robust solution - it checks for any reference to the basename in any text file. Note the solutions above that didn't include storyboard files (completely understandable, they didn't exist at the time).

Ack makes this pretty fast, but there are some obvious optimizations to make if this script runs frequently. This code checks every basename twice if you have both retina/non-retina assets, for example.

#!/bin/bash

for i in `find . -name "*.png" -o -name "*.jpg"`; do 
    file=`basename -s .jpg "$i" | xargs basename -s .png | xargs basename -s @2x`
    result=`ack -i "$file"`
    if [ -z "$result" ]; then
        echo "$i"
    fi
done

# Ex: to remove from git
# for i in `./script/unused_images.sh`; do git rm "$i"; done

Tags:

Xcode

Assets