31

I have an automation running in the iOS Simulator that I have to remove before another run. How do I remove the app from the iOS Simulator from the command line?

For each simulator device directory (located at ~/Library/Developer/CoreSimulator/Devices/*), I tried to delete ./data/Containers/Bundle/Application/ and ./data/Containers/Data/Application/.

Even when I tried to delete the app by long pressing the app in the Simulator (the app becomes jiggly) and click on the X button, the user defaults were not being cleared. I want the app state to be 100% clean.

I found a good solution to solve this problem.

5 Answers 5

59

With Xcode 6.1, to uninstall an app, use the following command:

xcrun simctl uninstall booted com.example.apple-samplecode.UICatalog

where com.example.apple-samplecode.UICatalog is the bundle identifier of the application you wish to uninstall.

6
  • This is a much cleaner implementation, especially with the inclusion of 'booted'
    – Glen T
    Feb 7, 2015 at 19:37
  • Is there a way to fail silently if there is no booted simulator?
    – tfe
    Jun 5, 2015 at 21:59
  • 2
    I guess I could simply do xcrun simctl uninstall booted com.example.apple-samplecode.UICatalog || true.
    – tfe
    Jun 5, 2015 at 22:02
  • 3
    When running on several simulator devices simultaneously, the one on which the app in uninstalled is undefined. In that case, use the UDID of the device instead of booted (get the available UDIDs with xcrun simctl list)
    – atineoSE
    Jan 10, 2017 at 14:11
  • 1
    exactly what we needed! I searched for method to wipe all contents while developing UITests every time it starts. Sep 17, 2020 at 9:09
23

One approach that we found for deleting user defaults is to delete all files in the ./data/Library/Preferences/* in addition to deleting application and data directories.

However, in Xcode 6, the command xcrun has new subcommand called simctl that allows me to manage iOS Simulator including resetting the simulator, and installing the application.

The solution that I came up with is to use the command

xcrun simctl erase [device ID]

Example

If xcrun simctl list() returns

9DDA0CFE-7CEC-40B6-A343-1EC01F282B22 (active, disconnected)
    Watch: Apple Watch Series 2 - 42mm (88474523-163E-4021-B591-2AECBFA26997) (Shutdown)
    Phone: iPhone 7 Plus (5785E680-15CD-42D3-82AB-597286A270C5) (Shutdown)

then run these 2 commands

xcrun simctl erase 88474523-163E-4021-B591-2AECBFA26997
xcrun simctl erase 5785E680-15CD-42D3-82AB-597286A270C5

() The device ID can be obtained from running

xcrun simctl list

This will reset the simulator (equivalent to iOS Simulator > Reset Contents and Settings... menu item).

With Xcode 6.0.1 (Build 6A317), there is either a bug or a change in behavior where when you uninstall an application, user defaults are not removed.

Usage: simctl [--noxpc] [--set <set path>] <subcommand> ... | help [subcommand]
Command line utility to control the iOS Simulator

For subcommands that require a <device> argument, you may specify a device UDID
or the special "booted" string which will cause simctl to pick a booted device.
If multiple devices are booted when the "booted" device is selected, simctl
will choose one of them.

Subcommands:
    create        Create a new device.
    delete        Delete a device.
    erase         Erase a device's contents and settings.
    boot          Boot a device.
    shutdown      Shutdown a device.
    rename        Rename a device.
    getenv        Print an environment variable from a running device.
    openurl       Open a URL in a device.
    addphoto      Add a photo to the photo library of a device.
    install       Install an app on a device.
    uninstall     Uninstall an app from a device.
    launch        Launch an application by identifier on a device.
    spawn         Spawn a process on a device.
    list          List available devices, device types, or runtimes.
    notify_post   Post a darwin notification on a device.
    icloud_sync   Trigger iCloud sync on a device.
    help          Prints the usage for a given subcommand.
3
  • If all you want to do is delete the single app, you should use uninstall instead of erase. Oct 18, 2014 at 14:24
  • 1
    In Xcode 6.0.1, even if you use uninstall command, the user defaults remain intact (it's a bug). That's why I suggest using erase.
    – Frank
    Oct 21, 2014 at 21:52
  • Ah, yes. Good point, however that is not an issue with Xcode 6.0.1 but with iOS 8.0. If you use 'delete' on a simulated 7.x device, it will work fine. Oct 22, 2014 at 0:57
11

Reset all Content & Settings in a single command

  1. Quit iPhone Simulator
  2. In Terminal, run:

    xcrun simctl erase all
    

This will reset content and settings of all the simulators for the active version of Xcode (the one referenced by xcode-select -p).

0
5
xcrun simctl uninstall simulatorIdentifier appBundleId
0

I know, this is a decade-old question, but things have changed since, though the necessity to test on clean installation is still there. None of the answers above seem quite satisfactory to me so I propose another solution.

I propose a method safe with respect to different device/simulator ids as well as bundle id:

if [[ "$RUN_DESTINATION_DEVICE_PLATFORM_NAME" == "iphonesimulator" ]]; then
    xcrun simctl uninstall $RUN_DESTINATION_DEVICE_UDID $PRODUCT_BUNDLE_IDENTIFIER
else
    xcrun devicectl device uninstall app --device $RUN_DESTINATION_DEVICE_UDID $PRODUCT_BUNDLE_IDENTIFIER
fi

To automate it and run as a part of your development workflow:

  1. Add a custom scheme, let's say CleanInstall:

enter image description here

  1. Add New Run Script Action to Run phase's Pre-actions:

enter image description here

enter image description here

  1. Make sure to "Provide build settings from" your target to gain access to $PRODUCT_BUNDLE_IDENTIFIER which is inaccessible at Run phase as is:

enter image description here

  1. Paste script above

Resulting setup should look like this:

enter image description here


Design considerations, differences from other methods:

Pros:

  1. Fully automated: no need for manual touches on device/simulator, or manual terminal commands invocation
  2. Doesn't require third-party tools like libmobiledevice
  3. Doesn't require you to remember or even know bundle id or destination device/simulator id. No need for xcrun simctl list, Xcode provides everything for you
  4. Erases only the required app, doesn't require you to do a full wipe of simulator unlike xcrun simctl erase (arguably a pro or con depending on your situation, see below)
  5. Erases an app exactly from destination device/simulator specified in Xcode when running an app, unlike xcrun simctl uninstall booted where there could be multiple devices/simulators opened/connected at the same time and behaviour may be kind of undefined
  6. Naturally extends to unit testing both from Xcode UI and command-line assuming you specify the scheme

Cons:

  1. To my knowledge this doesn't (and shouldn't) erase keychain items. So unless you're testing keychain items/groups you should be fine.
  2. While researching this question I realised there are some apps that rely on safari caches (WKWebView users?) and there are chances that safari caches might be unaffected by only app deletion and persist after that?

In both of these cases feel free to create another scheme similar to the above with something like xcrun simctl erase $RUN_DESTINATION_DEVICE_UDID. Be careful with real devices though!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.