r/iOSProgramming • • 3d 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!

6 Upvotes

10 comments sorted by

1

u/cuongnt3010 2d ago

yes it checks in .xcassets image resource. you can use ai use tool call like this cli to clean.

1

u/dawedev 2d ago

For the re-exported PNG case, a perceptual hash (dHash on a small grayscale copy) catches most of them, then just list the pairs for a human to confirm. Byte-exact matching misses nearly every duplicate a designer hands over twice.

0

u/cuongnt3010 2d ago

Thank you for your suggestion. I’ll investigate applying it instead of using bytes.

1

u/Ok_Breakfast_9531 2d ago

one case i didn't see in the list: folders inside the catalog with "provides namespace" ticked. then the code says Image("onboarding/hero") and the generated symbol is nested too, so a scanner that only looks for "hero" would flag it as unused. does the audit resolve the folder prefix, or is that a job for the toml globs?

1

u/cuongnt3010 1d ago

I’ve updated to the latest version. If you’re interested in this project, please contribute.

1

u/LongjumpingCandle738 3d ago

Does it check ImageResource ?
Anyway, I think directly asking AI to clean is much more effective

1

u/Sea_Wrongdoer_3500 2d ago

nice, dry run by default + refusing to run on a dirty tree is the right call imo

one thing that would probably trip it up: enums with implicit raw values. like enum Icon: String { case logo, avatar } and then Image(icon.rawValue) somewhere. there's no "logo" string literal anywhere so it'd show up as unused

same with swiftgen/r.swift if the generated file is gitignored and only created at build time, code just has Asset.logo and never the actual name

how does it compare to fengniao btw? thats what i used to see people recommend for this

1

u/Difficult_Duty2198 2d ago

does it catch images that are only referenced through a built string, like Image("icon_(name)")? those always seem to be the ones that get wrongly flagged as unused.

0

u/ThanhDongNguyen 3d ago

Dry run by default and refusing with uncommitted changes is the right call.

Few cases I'd check before trusting --apply:

  • AppIcon and AccentColor are referenced from build settings, not code, same for alternate icons listed in Info.plist or the "alternate app icon sets" setting
  • names built from enum raw values, like Image(asset.rawValue), you can't see them as literals
  • widget and other extension targets that use the main app's catalog
  • color sets, not only image sets

Maybe keep a small allowlist file in the repo for these, so CI doesn't fail on them every time.