Unused code cleanup in iOS projects

August 28, 2019

#xcode #workflow #optimization

As your project evolves there might appear unreachable code that is not used anymore or will never be reached. Apart from cluttering the project, it might have an impact on a project’s build time by slowing it down. Therefore, it’s useful to know how to find and remove unused code from your project.

In this article I’ll go over a few tools which you can use to clean up your project:

  • Xcode and AppCode
  • Swift-scripts
  • Periphery

Finding unused code with Xcode and AppCode

Xcode and AppCode are designed to be a default tool that helps you keep your code accurate and clean. Unfortunately, both IDEs don’t provide that many options to find unreachable code written in Swift. AppCode works better with Objective-C code, detecting automatically unused code and suggesting the “Safe delete” quick-fix. It can help you find unused import directives as well. You can adjust AppCode introspection options via application preferences.

AppCode settings image

It might be useful to turn on compiler warning flags like -Wunused-variable, -Wunused-label, etc. or -Wall that aggregates all of these. This way you can get a warning about some of the unused declarations in your code.

Cleaning up unused code with Swift-scripts

Swift-scripts is an open-source repository available on Github that comes with a script written in Ruby. You can install it by cloning the repository and run via command line from the project’s directory:

cd <path-to-project>

As a result of running the script you will get a list of items:

unused.rb scan image

You can follow this list to remove unused code from your project. Unfortunately, you might get lots of false-positive results because the script just performs text search under the hood. So, it makes sense to verify the script’s output before doing a cleanup.

Removing unused declarations with Periphery

Periphery is an open-source tool written in Swift that aims to find unused declarations in your code. You can install one easily via homebrew:

brew tap peripheryapp/periphery
brew cask install periphery

After installation is complete, run the following command:

periphery scan

Periphery will start a guided setup, resulting in printing out the full scan command and executing one.

periphery scan image

Periphery implementation is based on SourceKit framework that helps finding the references between declarations. Due to this, it provides quite accurate results that might be used in further considerations.


There is no reason to keep dead code in your project, it needs to be found and removed. I’d suggest integrating Periphery in your CI pipeline or running it manually every 3-6 months. Let’s keep your project clean from unused code!

Thanks for reading!