Xcode 6 – Reset Simulators from the Command Line

In Xcode 6, apple made some interesting changes to the way iOS simulators work. They are actually really interesting. The short story is, you can create your own simulators. Pick your own OS and device hardware, and a simulator is born. All you have to do is open Xcode and go to  Window -> Devices and in the bottom left corner is a button to add new simulators.

Xcode6 Add SimulatorXcode6 Add Simulator Dialog

This is really cool. It allows you to delete simulators that you honestly don’t care for, and recreate them if ever you do. Or, perhaps a more valuable thing you could do, is create simulators on a per project basis. Specifically for things like continuous integration or automation. That way you can ensure that if you reset the content and settings for one app, it doesn’t do it for everything, just in case you have specific requirements for Core Data or NSUserDefaults.

However, this change has caused some confusion. People are confused about where the documents directory is (link). And there are plenty of people offering advice on how to quickly find where your documents directory is for the simulator (link).

In addition, anyone who is used to running something like this

is in for one hell of a night, when they try this

See, inside of any device at  ~/Library/Developer/CoreSimulator/Devices/ is a file called  device.plist. And If you look inside, you’ll see something like this

And as you can clearly see, the last key in the dictionary is the state. That right there should be a dead giveaway that something, somewhere is expecting things to be left as they are. And that is the case. If you just start deleting things off of your filesystem, you’ll break the simulator(s).

However, if you break things in just the right way, you can stumble upon a magical new command line tool.

simctl


simctl is a new command line tool that you can use to, as the name suggests, control the simulators. Like many tools in Xcode you run it by evoking

This will give you the usage, and you’ll see that you can do a whole lot with the tool. And finally there is an easy way to reset the simulator from the command line.

First, you need to find out the GUID/UUID of the device you want to reset.

which will output something like the following

Under the devices section you’ll see the GUID/UUID of the device is enclosed in parenthesis. Copy the GUID/UUID for the device you want to reset and run  xcrun simctl erase <device>. For example, the following would reset the iPhone 4s (iOS 7.0)

Do keep in mind that simctl will not let you reset a device that is currently running. Instead it will output an error:

This command line tool should make things a lot easier for people who have been trying to make Apple Scripts to click through the UI and reset all devices one at a time. It would be fairly easy to create a script to loop through all of the devices to reset them all.

2 thoughts on “Xcode 6 – Reset Simulators from the Command Line

  1. You are really cool.
    Sometimes, simulator devices are invalid, then I have to reset them. And you provide a beautiful answer.

Leave a Reply

Your email address will not be published. Required fields are marked *