How can I reset the iOS Simulator from the command line?
Asked Answered
W

18

94

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.

Whippoorwill answered 26/2, 2011 at 5:23 Comment(2)
Invoke the "Reset Content and Settings" menu itemWhippoorwill
May I kindly suggest to change the accepted answer to that of @Kappe, namely Quit Simulator, then execute xcrun simctl erase all?Filamentous
A
175

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
Alves answered 9/7, 2015 at 13:22 Comment(6)
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 ?Goatskin
maybe a sudo xcrun simctl erase allAlves
You need more upvotes. But an improvement: killall "Simulator" 2> /dev/null; xcrun simctl erase allGourmet
yes! the new improved version is awesome. thanks and nice work!Ostium
@Goatskin that state means the simulator is active and needs to be shutdown before resetting. try xcrun simctl shutdown all before xcrun simctl erase allKeli
Works for Xcode 12.4 also. I got this problem after I delete all the junks in Xcode folder, cash, logs, simulators etcHoarsen
R
60

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

Rattlebox answered 11/9, 2014 at 0:20 Comment(5)
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.Pokelogan
This answer should be upvoted, as it's the most recent solutionMastership
For erasing all simulators, answer from 'Joe Susnick' & its comment is usefulAbott
xCode 7 also has an all argument. So you can erase all simulators with xcrun simctl erase allAnastassia
@JeremyHuddlestonSequoia Wish I've seen this post and your comment earlier. I'm reinstalling Xcode right now :(Vivienne
W
37

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
Whippoorwill answered 26/2, 2011 at 17:45 Comment(6)
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".Labour
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.Majewski
It looks like this system preference may have moved. Under Mavericks, I found it under System Preferences > Security & Privacy > Privacy > Accessibility.Traverse
in Mavericks, you might see an issue saying it can't find the Reset button. try click button 1 instead of click button "Reset"Termination
I am getting FunctionalTests/script:34:49: execution error: File iPhone Simulator wasn’t found. (-43), and FunctionalTest/ is the folder where script isAnarthria
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 resultsOstium
C
22

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

Crozier answered 28/4, 2018 at 12:49 Comment(1)
Thank you. Solved issue by running: xcrun simctl shutdown all; xcrun simctl erase allDolce
B
16

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
Boger answered 22/10, 2014 at 20:12 Comment(2)
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 uuidLeavetaking
See @Kappe: xcrun simctl erase allFilamentous
I
11

Delete the contents of

~/Library/Application Support/iPhone Simulator/<sdk revision>

And you're good to go.

Immoderacy answered 23/9, 2011 at 19:35 Comment(1)
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.Decadent
R
5

Upon installing Xcode I always create a keyboard shortcut for "Reset Content and Settings" in the simulator. An extremely useful time saver.

System Preferences > Keyboard > Shortcuts > App Shortcuts > "+"

In the Application picker, select "Other..." to open the app picker dialog.

In this dialog you're unable to "Show Package Contents" to explore a .app, so you'll need to use Go to Folder via Cmd-Shift-G. (First open the application drop down and select Other)

In the current version of Xcode, go to the path:

/Applications/Xcode/Contents/Developer/Applications

Select Simulator.app and press "Add"

For Menu Title, enter Reset Content and Settings...

For Keyboard Shortcut, press CMD-Shift-R

Reset Content and Settings

Rhigolene answered 3/3, 2016 at 0:45 Comment(2)
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..."Ostium
@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.Rhigolene
F
4

The keyboard short-cut solution is not relevant anymore and unfortunately @Cameron solution didn't work for me either (I tried to debug it with no luck)

Here is what works for me:

#!/bin/bash

# `menu_click`, by Jacob Rus, September 2006
# 
# Accepts a list of form: `{"Finder", "View", "Arrange By", "Date"}`
# Execute the specified menu item.  In this case, assuming the Finder 
# is the active application, arranging the frontmost folder by date.

osascript <<SCRIPT

on menu_click(mList)
    local appName, topMenu, r

    -- Validate our input
    if mList's length < 3 then error "Menu list is not long enough"

    -- Set these variables for clarity and brevity later on
    set {appName, topMenu} to (items 1 through 2 of mList)
    set r to (items 3 through (mList's length) of mList)

    -- This overly-long line calls the menu_recurse function with
    -- two arguments: r, and a reference to the top-level menu
    tell application "System Events" to my menu_click_recurse(r, ((process appName)'s ¬
        (menu bar 1)'s (menu bar item topMenu)'s (menu topMenu)))
end menu_click

on menu_click_recurse(mList, parentObject)
    local f, r

    -- `f` = first item, `r` = rest of items
    set f to item 1 of mList
    if mList's length > 1 then set r to (items 2 through (mList's length) of mList)

    -- either actually click the menu item, or recurse again
    tell application "System Events"
        if mList's length is 1 then
            click parentObject's menu item f
        else
            my menu_click_recurse(r, (parentObject's (menu item f)'s (menu f)))
        end if
    end tell
end menu_click_recurse

application "iPhone Simulator" activate    
menu_click({"iPhone Simulator", "iOS Simulator", "Reset Content and Settings…"})

tell application "System Events"
    tell process "iPhone Simulator"
        tell window 1
            click button "Reset"
        end tell
    end tell
end tell

SCRIPT
Form answered 11/2, 2013 at 11:53 Comment(3)
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 tellEupatrid
You also have to enable "Universal Access" from the System preferencesEupatrid
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)Eupatrid
D
1

I wrote a script that will reset the contents & settings of all versions and devices for the iOS Simulator. It grabs the device names and version numbers from the menu, so it will include any new devices or iOS versions that Apple releases simulators for.

It's easy to run manually or use in a build-script. I would suggest adding it as a Pre-Action Run Script before the build.

It's based heavily on Stian's script above, but doesn't become stale with new iOS versions, and eliminates the dialog box (better for automation build scripts and working from the command-line).

https://github.com/michaelpatzer/ResetAllSimulators

Drucill answered 18/10, 2013 at 17:17 Comment(0)
B
1

I have found this very helpful tool called "SimulatorManager": http://tue-savvy.github.io It will reset all you simulators by means of a menu bar widget (not sure if that's what its called) but on top of that it will give you quick access to all your application data. I really can't live without it anymore. Spread the word!

Backsaw answered 15/12, 2015 at 23:57 Comment(0)
T
0

I want to add something to Cameron Brown's answer. To make sure the correct version is resetted (e.g, iPad, version 6.1), I start the iOS Simulator via ios-sim:

version=$(echo "$DESTINATION" | egrep -o "OS=[0-9.]{3}" | cut -d '=' -f 2)
simType=$(echo "$DESTINATION" | egrep -o "name=[a-zA-Z]*" | cut -d '=' -f 2 | tr "[A-Z]" "[a-z]")

IOS_SIM_BIN=$(which ios-sim)

if [ -z $IOS_SIM_BIN ]
then
    echo "ios-sim not installed, please use 'sudo npm install ios-sim -g'"
fi    

echo "Resetting Simulator \"$simType\", version \"$version\""

$IOS_SIM_BIN start --family $simType --sdk $version --timeout 1
osascript /path/to/reset_simulator.applescript

$DESTINATION can be e.g "OS=7.0,name=iPad".

For that to work correctly, I adapted the reset_simulator.applescript a bit and removed the activation part:

tell application "iPhone Simulator"
    activate
end tell
Tanatanach answered 8/10, 2013 at 10:14 Comment(0)
O
0

As an added bonus to using the xcrun commands you can launch a device after you listed with

xcrun simctl list

Once you have the list displayed run:

xcrun instruments -w "iPhone 5s (8.1 Simulator) [31E5EF01-084B-471B-8AC6-C1EA3167DA9E]"
Ostium answered 9/12, 2014 at 1:47 Comment(0)
P
0

We use the following python script to reset the simulator on our build server.

#!/usr/bin/env python 

import subprocess
import re
import os

def uninstall_app():
    print 'Running %s' % __file__

    # Get (maybe read from argv) your bundle identifier e.g. com.mysite.app_name
    try:
        bundle_identifier = '$' + os.environ['BUNDLE_IDENTIFIER']
    except KeyError, e:
        print 'Environment variable %s not found. ' % e
        print 'Environment: ', os.environ
        exit(1)

    print 'Uninstalling app with Bundle identifier: ', bundle_identifier

    # We call xcrun, and strip the device GUIDs from the output
    process = subprocess.Popen(['xcrun', 'simctl', 'list'], stdout=subprocess.PIPE)

    # Read first line
    line = process.stdout.readline()

    while True:

        # Assume first match inside parenthesis is what we want
        m = re.search('\((.*?)\)', line)

        if not (m is None):

            # The regex found something, 
            # group(1) will throw away the surrounding parenthesis
            device_GUID = m.group(1)

            # Brutely call uninstall on all listed devices. We know some of these will fail and output an error, but, well..            
            subprocess.call(['xcrun', 'simctl', 'uninstall', device_GUID, bundle_identifier])

        # Read next line
        line = process.stdout.readline()

        # Stop condition
        if line == '':
            break

if __name__ == '__main__':
    uninstall_app()

It assumse your app's bundle identifier is set as an environment variable, e.g.

export BUNDLE_IDENTIFIER=com.example.app_name

maybe you'd want to pass the bundle identifier in another way.

Peccable answered 18/4, 2015 at 14:24 Comment(0)
C
0

Building on most of the answers above, I am using Keyboard Maestro and made a little Macro to reset the currently running Simulator and restarting it. It turns the focus back to Xcode after resetting and restarting, so I can hit Command+R again right away to re-run the app, which I find very convenient.

enter image description here

The content of the ruby script is:

#!/usr/bin/env ruby

list = `xcrun simctl list`.split("\n")

list.each do |line|
  if line =~ /\(Booted\)$/
    device = line.match(/([^(]*)\s+\(([^)]*)\)\s+\(([^)]*)\).*/)[1]
    uuid = line.match(/([^(]*)\s+\(([^)]*)\)\s+\(([^)]*)\).*/)[2]
    status = line.match(/([^(]*)\s+\(([^)]*)\)\s+\(([^)]*)\).*/)[3]
    puts uuid
    break
  end
end
Cubism answered 30/7, 2015 at 7:45 Comment(0)
V
0

Here's a Rakefile task to reset a targeted simulator. This works with Xcode 7 since the Xcode 7 command line tools broke the xcrun simctl uninstall command. I have a little custom runC method since I like to see the actual terminal command as well as its output.

desc "Resets the iPhone Simulator state"
task :reset_simulator => [] do
  deviceDestinationName = 'iPhone 6' #for efficiency since we only target one device for our unit tests
  puts "...starting simulator reset"
  runC('killall "iOS Simulator"')
  runC('killall "Simulator"')
  runC('xcrun simctl list > deviceList.txt')
  lines = File.open('deviceList.txt').readlines
  lines.each do |line|
    lineStripped = line.strip
    if (lineStripped=~/[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12}/)
      if (lineStripped !~ /unavailable/ && lineStripped.include?("#{deviceDestinationName} ("))
        puts "Inspecting simulator: #{lineStripped} by making sure it is shut down, then erasing it."
        needsShutdown = !lineStripped.include?('Shutdown')
        aDeviceId = lineStripped[/[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12}/]
        if (needsShutdown)
          runC("xcrun simctl shutdown #{aDeviceId}")
        end
        runC("xcrun simctl erase #{aDeviceId}")
        #does not work to just uninstall the app with Xcode 7, do just rely on resetting the device above
        #`xcrun simctl uninstall #{aDeviceId} com.animoto.AppServiceClientTester`
      end
    end
  end
  runC('rm deviceList.txt')
end

#Runs a command and prints out both the command that will be run and the results
def runC(command)
  puts '$ ' + command
  puts `#{command}`  
end
Vestal answered 6/10, 2015 at 21:22 Comment(0)
C
0

Just one liner command : Loop through each simulator in Devices folder and erase.

for id in $(xcrun simctl list devices | grep -E '([0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12})' -o); do xcrun simctl erase $id; done

paste it in Terminal and hit return.

Breaking it down:

  • xcrun simctl list devices: Lists all available simulators.
  • grep -E '([0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12})' -o: Matches the UUIDs of simulators.
  • for id in $(...): Loops through each UUID found.
  • xcrun simctl erase $id: Erases the simulator corresponding to the current UUID.
Coruscate answered 3/5 at 10:21 Comment(0)
S
-1

target names and Simulator app name seem to have changed a bit to xCode6 / iOS8. Here is un updated version of Cameron Brown's osascript for xCode6 / iOS8:

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
Soilure answered 26/9, 2014 at 8:3 Comment(1)
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 SSHSoilure
I
-5

I present,

The Definitive iOS Simulator Reset Script (link)

enter image description here

Based on Oded Regev's code (which was based on Jacob Rus's fine "menu_click" code)

Inlaid answered 6/3, 2013 at 13:22 Comment(2)
Okay, we get it. Try adding comments with this where questions have already accepted answers.Briticism
Great, though that doesn't work solely from the command line, which was the question. Nice work though :)Segalman

© 2022 - 2024 — McMap. All rights reserved.