How can I reset iOS Simulator from the command line?

I need to reset the iPhone simulator a lot, and have not found a way to do this without using a mouse. This is a trifle, but it really pains me to do it, and I would like it to be done using the keyboard shortcut.

It would be even better to use reset from the command line, so I could create a reset in the deployment of the script.

I am not very familiar with iOS or MacOS.

+58
workflow ios simulator
Feb 26 '11 at 5:23
source share
17 answers

Just run this in the terminal:

xcrun simctl erase all 

improvement suggested by @txulu, just kill the simulator before doing the cleanup:

 killall "Simulator" 2> /dev/null; xcrun simctl erase all 
+74
Jul 09 '15 at 13:22
source share

In Xcode 6, DO NOT REMOVE THE SIMULATOR FOLDER! He will blame things, and this will lead to a headache.

Xcode 6 actually has a tool to control the simulator from the command line.

Verify that the command line options are set to Xcode 6

 xcrun simctl 

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

Team

 xcrun simctl list 

will show you all the devices that you configured. The result 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 parentheses and run xcrun simctl erase

eg,

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

will remove the device iOS 7.0, iPhone 5

+55
Sep 11 '14 at 0:20
source share

I think I would publish this for everyone who is faced with the same need. Someone from reddit gave me this solution (which I tested and it works fine). Please note that this time you will need the ellipsis after "Settings", and not three periods (strange).

This is AppleScript, which can be called from the command line in reset 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 call with:

 osascript /path/to/script 
+36
Feb 26 '11 at 17:45
source share

COPY RESPONSE - note: the contents and settings of all available simulators will be reset.

Thanks @Alpine for the inspiration and knowledge. If you run this on your command line, you can reset all 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 
+16
Oct 22 '14 at 20:12
source share

Delete Content

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

And you are good to go.

+11
Sep 23 '11 at 19:35
source share

I checked this with Xcode 9. To close all active simulators, do:

 xcrun simctl shutdown all 

To reset all simulators, do:

 xcrun simctl erase all 

You can filter the simulator to close / reset as follows:

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

Find all available simulators (and their GUIDs) on your computer as follows:

 xcrun instruments -s 

To run any simulator by GUID:

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

To install the application on a downloaded simulator:

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

To remove an application from a loaded simulator:

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

To run the application in a loaded simulator:

 xcrun simctl launch booted "com.app.bundleIdentifier" 

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

+7
Apr 28 '18 at 12:49
source share

The shortcut solution for the keyboard is no longer suitable, and unfortunately the @Cameron solution also does not work for me (I tried to debug it without any 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 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 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) ¬ (menu bar 1) (menu bar item topMenu) (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 length > 1 then set r to (items 2 through (mList length) of mList) -- either actually click the menu item, or recurse again tell application "System Events" if mList length is 1 then click parentObject menu item f else my menu_click_recurse(r, (parentObject (menu item f) (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 
+4
Feb 11 '13 at 11:53
source share

After installing Xcode, I always create a shortcut for the "Reset Content and Settings" keys in the simulator. Very useful time saver.

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

In the Select application, select Other ... to open the application selection dialog.

In this dialog box, you cannot "Show package contents" to examine .app, so you need to use Go to Folder via Cmd - Shift - G. (First open the application and select Other )

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

/Applications/Xcode/Contents/Developer/Applications

Select Simulator.app and click "Add"

For Menu Title enter Reset Content and Settings...

For Keyboard Shortcut press Cmd - Shift - R

Reset Content and Settings

+4
Mar 03 '16 at 0:45
source share

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

Easy to launch manually or used in build-script. I would suggest adding it as a preliminary script action before building.

It relied heavily on the Stian script above, but does not become obsolete with newer versions of iOS and eliminates the dialog box (better for build scripts and command line scripts).

https://github.com/michaelpatzer/ResetAllSimulators

+1
Oct 18 '13 at 17:17
source share

I found this very useful tool called "SimulatorManager": http://tue-savvy.imtqy.com This will reset all your simulators using the menu bar widget (not sure what it is called), but in addition it will give quick access to all your application data. I can no longer live without him. Put the word!

+1
Dec 15 '15 at 23:57
source share

I want to add something to Cameron Brown's answer . To make sure that the correct version is reset (e.g. iPad, version 6.1), I launch 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 "[AZ]" "[az]") 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, for example, "OS=7.0,name=iPad" .

For proper operation, I slightly adjusted reset_simulator.applescript and removed the activation part:

 tell application "iPhone Simulator" activate end tell 
0
Oct 08 '13 at 10:14
source share

As an added bonus to using xcrun commands, you can start the device after you have specified with

 xcrun simctl list 

After you have looked at the list:

 xcrun instruments -w "iPhone 5s (8.1 Simulator) [31E5EF01-084B-471B-8AC6-C1EA3167DA9E]" 
0
Dec 09 '14 at 1:47
source share

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 eg 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 is assumed that your application package identifier is set as an environment variable, for example.

 export BUNDLE_IDENTIFIER=com.example.app_name 

Perhaps you need to pass the package identifier in another way.

0
Apr 18 '15 at 14:24
source share

Based on most of the answers above, I use Keyboard Maestro and made a small macro to reset the current simulator and restarted it. It returns focus back to Xcode after reset and restart, so I can press Command+R again to start the application again, which is very convenient for me.

enter image description here

Content ruby ​​script:

 #!/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 
0
Jul 30 '15 at 7:45
source share

Here's the Rakefile task to reset the target simulator. This works with Xcode 7 because the Xcode 7 command line tools violated the xcrun simctl uninstall command. I have a small 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 
0
Oct 06 '15 at 21:22
source share

Simulator's target and app names seem to have changed a bit on xCode6 / iOS8. The following is an updated version of osascript Cameron Brown 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 
-one
Sep 26 '14 at 8:03
source share

I imagine

Ultimate iOS Simulator Reset Script (link)

enter image description here

Based on the Oded Regev code (which was based on the "menu_click" code from Jacob Rus)

-5
Mar 06 '13 at 13:22
source share



All Articles