How can I reset the iOS Simulator from the command line?

44,729

Solution 1

Just run this in the terminal:

xcrun simctl shutdown all && xcrun simctl erase all

Another approach suggested by @txulu, before erasing the simulator kill the process:

killall "Simulator" 2> /dev/null; xcrun simctl erase all

Solution 2

In Xcode 6, DO NOT JUST DELETE THE FOLDER FOR THE SIMULATOR! It WILL screw things up, and it WILL cause you a headache.

In Xcode 6, there's actually a tool to control the simulator from the command line.

Make sure your command line settings are set to Xcode 6

xcrun simctl

In Xcode 6, each device has a GUID/UUID associated to it, to reset a specific device, you need the GUID for it.

The command

xcrun simctl list

will show you all of the devices you have set up. The output will look like this:

== Devices ==
-- iOS 7.0 --
iPhone 4s (F77DC0AE-6A6D-4D99-9936-F9DB07BBAA82) (Shutdown)
iPhone 5 (5B78FC0D-0034-4134-8B1F-19FD0EC9D581) (Shutdown)
iPhone 5s (569E5910-E32D-40E2-811F-D2E8F04EA4EF) (Shutdown)
iPad 2 (451DBBD8-A387-4E77-89BF-2B3CD45B4772) (Shutdown)
iPad Retina (2C58366B-5B60-4687-8031-6C67383D793F) (Shutdown)
iPad Air (50E03D3B-3456-4C49-85AD-60B3AFE4918B) (Shutdown)
-- iOS 7.1 --
-- iOS 8.0 --
iPhone 4s (27818821-A0BB-496E-A956-EF876FB514C2) (Shutdown)
iPhone 5 (6FBAA7E2-857C-432A-BD03-980D762DA9D2) (Shutdown)
iPhone 5s (7675C82B-DE49-45EB-A28D-1175376AEEE9) (Shutdown)
iPad 2 (836E7C89-B9D6-4CC5-86DE-B18BA8600E7B) (Shutdown)
iPad Retina (EFDD043D-2725-47DC-A3FF-C984F839A631) (Shutdown)
iPad Air (9079AD6C-E74D-4D5F-9A0F-4933498B852E) (Shutdown)
Resizable iPhone (943CFEDE-A03C-4298-93E3-40D0713652CB) (Shutdown)
Resizable iPad (DBA71CA5-6426-484B-8E9B-13FCB3B27DEB) (Shutdown)

Just copy the GUID from inside the parentheses, and run xcrun simctl erase

for example,

xcrun simctl erase 5B78FC0D-0034-4134-8B1F-19FD0EC9D581

would erase the iOS 7.0, iPhone 5 device

Solution 3

Thought I'd post this for anyone who runs into the same need. Someone on reddit gave me this solution (which I tested and it works great). Note that this time you need an ellipsis after "Settings", not three periods (weird).

This is an AppleScript that can be invoked from the command line to reset the Simulator:

tell application "iPhone Simulator"
    activate
end tell

tell application "System Events"
    tell process "iPhone Simulator"
        tell menu bar 1
            tell menu bar item "iOs Simulator"
                tell menu "iOs Simulator"
                    click menu item "Reset Content and Settings…"
                end tell
            end tell
        end tell
        tell window 1
            click button "Reset"
        end tell
    end tell
end tell

Save as /path/to/script and invoke with:

osascript /path/to/script

Solution 4

COPY-PASTE ANSWER - note: will reset the contents and settings of all available simulators.

Thanks @Alpine for the inspiration and knowledge. If you run this in your command line you should be able to reset all the available sims. This works with Xcode 6.

# Get the sim list with the UUIDs
OUTPUT="$(xcrun simctl list)"
# Parse out the UUIDs and saves them to file
echo $OUTPUT | awk -F "[()]" '{ for (i=2; i<NF; i+=2) print $i }' | grep '^[-A-Z0-9]*$' > output.txt
# Iterate through file and reset sim
for UUID in `awk '{ print $1 }' output.txt`
do
xcrun simctl erase $UUID
done

Solution 5

I checked it with XCode 9. To close all active simulators run:

xcrun simctl shutdown all

To reset all simulators run:

xcrun simctl erase all

You can filter what simulator to close/reset like this:

xcrun simctl shutdown F36B238F-3ED6-4E10-BB5A-0726151916FA
xcrun simctl erase F36B238F-3ED6-4E10-BB5A-0726151916FA

Find all accessible simulators (and their GUID) on your machine like this:

xcrun instruments -s

To run any simulator by GUID:

xcrun instruments -w F36B238F-3ED6-4E10-BB5A-0726151916FA -t Blank

To install app to the booted simulator:

xcrun simctl install booted /path/to/your.app

To remove app from the booted simulator:

xcrun simctl uninstall booted /path/to/your.app

To launch the app in the booted simulator:

xcrun simctl launch booted "com.app.bundleIdentifier"

"com.app.bundleIdentifier" is your CFBundleIdentifier in Info.plist

Share:
44,729
Cameron Brown
Author by

Cameron Brown

Game developer and enthusiastic amateur musician!

Updated on July 21, 2022

Comments

  • Cameron Brown
    Cameron Brown almost 2 years

    I need to reset the iPhone Simulator a lot, and haven't found a way to do it without using the mouse. It's a small thing, but I'm really sick of doing it and would love to have a way to do this using a keyboard shortcut.

    Even better would be a way to reset it from the command line, so I could build a reset into a deploy script.

    I am not very familiar with iOS or MacOS.

  • nschum
    nschum about 12 years
    If you get "System Events got an error: Access for assistive devices is disabled.", make sure to enable it under System Preferences under "Universal Access".
  • RPM
    RPM over 11 years
    The above script works really well. If you want to give the user the option to not Reset it (you shouldnt have the need to) then comment out the following block : tell application "System Events" tell process "iPhone Simulator" tell window 1 click button "Reset" end tell end tell end tell
  • RPM
    RPM over 11 years
    You also have to enable "Universal Access" from the System preferences
  • Admin
    Admin over 11 years
    Okay, we get it. Try adding comments with this where questions have already accepted answers.
  • RPM
    RPM about 11 years
    At times I get this error nowadays: resetSimulator.scpt:1238:1263: execution error: System Events got an error: Can’t get window 1 of process "iOS Simulator". Invalid index. (-1719)
  • Graham Perks
    Graham Perks almost 11 years
    Great, though that doesn't work solely from the command line, which was the question. Nice work though :)
  • i4niac
    i4niac over 10 years
    Not sure on this one. After running this command I start getting error message "iOS Simulator failed to install the application." when trying to run app from Xcode on simulator. The error persists until I reset simulator the "proper" way. It happens for 5.1 simulator in my case.
  • Roderic Campbell
    Roderic Campbell over 10 years
    This mixed with alfredApp and Automator and I'm resetting the sim like a champ. You can save the Automator work flow as an application and run in about 4 keystrokes.
  • Chris Vasselli
    Chris Vasselli about 10 years
    It looks like this system preference may have moved. Under Mavericks, I found it under System Preferences > Security & Privacy > Privacy > Accessibility.
  • kevinl
    kevinl almost 10 years
    in Mavericks, you might see an issue saying it can't find the Reset button. try click button 1 instead of click button "Reset"
  • Litome
    Litome almost 10 years
    Also note that Jenkins automated builds seem to fail on the tests target due to not connecting with the Simulator (I can see the simulator launching fine but not the app running in it). Dealt with in seprate question: Timeout when running xcodebuild tests under Xcode 6 via SSH
  • Jeremy Huddleston Sequoia
    Jeremy Huddleston Sequoia almost 10 years
    If you didn't head Alpine's warning and decided to delete the path out from under the running service, you can recover by just killing the running CoreSimulatorService (you need to SIGKILL) or rebooting.
  • seapy
    seapy over 9 years
    thank you. i make one line xcrun simctl list | awk -F "[()]" '{ for (i=2; i<NF; i+=2) print $i }' | grep '^[-A-Z0-9]*$' | xargs -I uuid xcrun simctl erase uuid
  • mokagio
    mokagio about 9 years
    This answer should be upvoted, as it's the most recent solution
  • lmiguelvargasf
    lmiguelvargasf about 9 years
    I am getting FunctionalTests/script:34:49: execution error: File iPhone Simulator wasn’t found. (-43), and FunctionalTest/ is the folder where script is
  • Kashif Hisam
    Kashif Hisam over 8 years
    For erasing all simulators, answer from 'Joe Susnick' & its comment is useful
  • viirus
    viirus over 8 years
    xCode 7 also has an all argument. So you can erase all simulators with xcrun simctl erase all
  • SwiftArchitect
    SwiftArchitect over 8 years
    See @Kappe: xcrun simctl erase all
  • WlkrShrpe
    WlkrShrpe almost 8 years
    Unable to erase contents and settings in current state: Booted Maybe there is something else I can pipe before/after your command to shutdown/reboot sim ?
  • Kappe
    Kappe almost 8 years
    maybe a sudo xcrun simctl erase all
  • Laser Hawk
    Laser Hawk almost 8 years
    Great tip! For those who can't access the Simulator.app path just apply it to 'All Applications'. Its not like any other app has "Reset Content and Settings..."
  • pkamb
    pkamb almost 8 years
    @AutomatorCreator Great point, I'll just do that in the future. I'll be happy not to find the Simulator app again, as its location seems to change on every Xcode release.
  • txulu
    txulu almost 8 years
    You need more upvotes. But an improvement: killall "Simulator" 2> /dev/null; xcrun simctl erase all
  • Laser Hawk
    Laser Hawk about 7 years
    Its stating the "System Events got an error: Can’t get button "Reset" of window 1 of process "Simulator". and switching it to click button 1 yields the same results
  • Laser Hawk
    Laser Hawk about 7 years
    yes! the new improved version is awesome. thanks and nice work!
  • pxpgraphics
    pxpgraphics over 5 years
    @WlkrShrpe that state means the simulator is active and needs to be shutdown before resetting. try xcrun simctl shutdown all before xcrun simctl erase all
  • winner.ktw
    winner.ktw about 4 years
    Thank you. Solved issue by running: xcrun simctl shutdown all; xcrun simctl erase all
  • clopex
    clopex about 3 years
    Works for Xcode 12.4 also. I got this problem after I delete all the junks in Xcode folder, cash, logs, simulators etc
  • Ranoiaetep
    Ranoiaetep almost 3 years
    @JeremyHuddlestonSequoia Wish I've seen this post and your comment earlier. I'm reinstalling Xcode right now :(