r/iOSProgramming • u/cuongnt3010 • 12m ago
Article I built a CLI to find unused and duplicate .xcassets images
On almost any iOS app that's been around for a couple years, `Assets.xcassets` turns into a junk drawer. Someone rips out an old screen and forgets the assets. Design hands over a tweaked icon under a slightly different name, and the old one just sits there forever.
nobody wants to clean it up by hand because Xcode won't warn you if an image is still used somewhere. You only find out after a build ships with a blank spot.
So I put together a small open-source CLI called `asset-guard` to audit `.xcassets` catalogs and optionally strip out unused `.imageset` folders before a build. It's written in Python, so you can run PR checks on a cheap Linux runner instead of burning macOS CI minutes.
If you have `uv` installed and want to run a read-only check on a project without installing anything permanently:
uvx --from git+https://github.com/nguyen-ta-cuong/asset-guard asset-guard audit /path/to/MyApp
A few choices I made because deleting a live image is way worse than leaving a dead one behind:
• Reference scanning leans conservative on purpose. It checks Swift and Obj-C string literals (with comments stripped), dynamic patterns like "icon_\(i)" and @"icon_%d", Xcode
15+ generated symbols (Image(.logo), UIImage.logo), plus storyboards, XIBs, and plists. It also picks up multiple .xcassets catalogs across targets and local packages while
skipping Pods, .build, DerivedData, and Carthage.
• clean is a dry run by default. Passing --apply deletes whole .imageset folders, and it refuses to run if your catalogs have uncommitted git changes so you can always revert.
• Byte-identical duplicates (matched via SHA-256) are flagged and can fail CI with --fail-on duplicates, though clean will never auto-delete them. Removing a duplicate safely
means rewriting code references, and I didn't want a pre-build script silently touching source files.
• It also flags broken image sets like bad Contents.json files and missing 2x, 3x scales.
A couple honest caveats right now. It only scans .imageset folders (.colorset and .symbolset aren't supported yet), and duplicate matching is byte-exact so visually identical re-exported PNGs won't show up. Also, if your app pulls asset names from a server or builds them with + concatenation, you'll need to put globs for those into .asset-guard.toml.
Repo (MIT licensed, includes a GitHub Action and Jenkinsfile example): https://github.com/nguyen-ta-cuong/asset-guard
If you've got an older Xcode project lying around, I'd love to see if audit throws any false positives on your setup. How are you all keeping .xcassets under control right now, or do dead images mostly just sit in the repo?
If you enjoy it, would you mind giving the repo a star? Thanks so much!