114

Is there a straightforward way to clean up the directory where xcode deploys an app when building for the iPhone simulator? I have a sqlite database that gets copied into the Documents folder on startup if necessary. The problem is that I might change my schema, but the new database won't get copied, because one already exists.

Ideally, every time I build, it would nuke the previous contents. Is this possible, or do I have to manually do it?

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Travis Jensen
  • 5,362
  • 3
  • 36
  • 40
  • Does the simulator support AppleScript? – Rog Mar 28 '09 at 08:00
  • Yes it does. I've written an AppleScript to iterate through all of the iOS simulators for each device and OS version and reset their Contents & Settings: https://github.com/michaelpatzer/ResetAllSimulators – M-P May 27 '14 at 14:24

18 Answers18

136

From Apples Dev Resources:

To set the user content and settings of the simulator to their factory state and remove the applications you have installed, choose Device > Erase All Content and Settings.

(On older versions: iPhone Simulator > Reset Content and Settings.)

j7nn7k
  • 17,995
  • 19
  • 78
  • 88
93

The simulator installs apps into:

"$HOME/Library/Application Support/iPhone Simulator/User/Applications"

Also check:

"$HOME/Library/Developer/CoreSimulator/Devices"

The GUID files and directories match up to the simulator's installed apps.

Manually delete all those files/directories to remove all applications from the simulator.

I know there is some way to add scripts to the build process in XCode.

Also it looks as if XCode changes the GUID it uses each build (the directory where my app sits changes between builds in XCode), so trying to delete the same directory all the time won't work. If you are only working on one app at a time then clearing out the entire directory would be an option.

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
PyjamaSam
  • 10,437
  • 3
  • 32
  • 20
  • 2
    Be sure to check out @idStar's answer down below. It has updated information regarding Lion/Xcode4 and a nice little script to help automate things. – PyjamaSam Jul 04 '12 at 15:19
  • I was testing adding/removing calendar subscriptions. On a real device, you can remove a calendar subscription in Settings > Accounts but this menu does not exist on iOS Simulator and I did not want to reset the whole simulator. So I ended up locally versioning my _Device_ folder with git and perform a `git reset HEAD --hard && git clean -f` to remove a calendar subscription after I added it. – Thibault D. Aug 05 '16 at 13:29
22

The way I do this is to simply click and hold on the icon for my app in the simulator--then when it starts to wiggle click the black and white (x). A message will pop up asking whether you really want to delete and you just click yes. The next time you build and deploy your app it will use the new sqlite db without a hitch and you don't have to go muck around in the filesystem.

John
  • 2,012
  • 2
  • 21
  • 33
  • This doesn't seem to work - it doesn't ACTUALLY delete data from the simulator? – Adam Nov 02 '09 at 22:42
  • 1
    My mistake - this works fine (confirmed by reading the directory in the accepted answer. I'd just forgotten Apple's user-unfriendly design of NSUserDefaults (unset values aren't reported by any sensible manner) – Adam Nov 02 '09 at 22:49
  • @quadelirus, I think your answer is the most appropriate in this situation. – datnt Jan 07 '13 at 03:15
  • This is what exactly i did, but i'm not sure how to close the app which has open already in simulator. Until unless I close the app, i could't get control on simulator. – Wanna Coffee Aug 23 '16 at 05:45
  • 1
    @WannaCoffee See http://stackoverflow.com/questions/18519799/ios-simulator-how-to-close-an-app – John Aug 24 '16 at 00:34
  • Thanks., this comment actually helps.!! The shortcut is ⌘+⇧+H, but you need to hit H twice in a row for it to simulate the double press that shows the apps. – Wanna Coffee Aug 24 '16 at 07:54
12

After iOS 5 on Mac OS X Lion, you can try:

  1. Create a script called RemoveSimulatorApps.command that contains:
    rm -rf "$HOME/Library/Application Support/iPhone Simulator/5.0/Applications/*"
    
  2. Save this script to a directory in your PATH.
  3. Make the file executable, such as:
    chmod +x RemoveSimulatorApps.command

Assumptions

  • You may want to invoke this from a keyboard favorites buttons, such as on a Logitech or Microsoft keyboard with programmable keys (hence, saving it as a .command file instead of say, .sh)
  • You are okay with blowing away everything in the iOS simulator (ideal if you're just actively working on one app)
  • All the notes from others apply about being a good upgradable app etc. (I personally found this useful nonetheless b/c I have development mode switches that reload a database in a specific state I was trying to do some consistent robustness/error handling on)
BryanH
  • 5,826
  • 3
  • 34
  • 47
idStar
  • 10,674
  • 9
  • 54
  • 57
8

it may be overkill but..

you can also use the menu and 'Reset Content and Settings...'

ShoeLace
  • 3,476
  • 2
  • 30
  • 44
7

for Xcode >= 6

xcrun simctl list | grep -oh '[A-Z0-9]\{8\}-[A-Z0-9]\{4\}-[A-Z0-9]\{4\}-[A-Z0-9]\{4\}-[A-Z0-9]\{12\}' | xargs -n1 xcrun simctl erase
Community
  • 1
  • 1
jia tian
  • 71
  • 1
  • 3
7

If you are using Xcode 9 -> Menubar -> Hardware -> Erase All Content and Settings

Xcode -> Hardware -> Erase All Content and Settings

Harald
  • 422
  • 5
  • 10
7

What you are really trying to do is to clear out your database, if you've changed the schema. One way to do this, and it would make you happier in the long run when you start shipping version 2.0, 3.0, etc. of your app, is to check the version of your sqlite table, and if it has changed, then discard the old file and use the one in your bundle.

Finding a way to clean up the Simulator won't help the real world problem of how to clean up a customer's iPhone when you ship a new version with a new schema.

For extra points, after determining that you have encountered an old schema, you may want to copy the new database over without destroying the old one, and load any interesting data out of the old database, into the new one. Then blow away the old database. That way you can preserve your user's additions to the database.

mahboudz
  • 39,196
  • 16
  • 97
  • 124
  • In general, I agree with you when you are talking about release upgrades, but in the midst of development of any particular version, this is far more overhead than is necessary. Database schema can change on a per-build basis, and needing to write conversion code for every build is just wrong. – Travis Jensen Apr 04 '09 at 21:02
5

As of Xcode 6, you can do this from the command line with: xcrun simctl erase

Also, the iOS Simulator app (both the Xcode 6 version and older versions) has a menu item called "Reset Content and Settings" that can be used to erase the currently booted device.

Jeremy Huddleston Sequoia
  • 22,938
  • 5
  • 78
  • 86
5

This works with Xcode 6:

xcrun simctl list | grep -oh '[A-Z0-9]\{8\}-[A-Z0-9]\{4\}-[A-Z0-9]\{4\}-[A-Z0-9]\{4\}-[A-Z0-9]\{12\}' | xargs -I{} xcrun simctl erase {}

For .bash_profile

alias cleansim="xcrun simctl list | grep -oh '[A-Z0-9]\{8\}-[A-Z0-9]\{4\}-[A-Z0-9]\{4\}-[A-Z0-9]\{4\}-[A-Z0-9]\{12\}' | xargs -I{} xcrun simctl erase {}"
Thomas T
  • 122
  • 2
  • 9
5

The following command in the terminal will wipe all of your simulators as if you had just installed them.

$xcrun simctl erase all

Just close simulators before you run it.

Codetard
  • 2,441
  • 28
  • 34
Albert Renshaw
  • 17,282
  • 18
  • 107
  • 195
4

As of Xcode 6:

xcrun simctl erase <sim udid> <- resets the simulator.

TinyTimZamboni
  • 5,275
  • 3
  • 28
  • 24
1

Clear Xcode Cache;

Command+Option+Shift+K

Command+Shift+K

(Use both of them because they have different functionality)

Clear Derived Data content ;

Menu Bar -> Window -> Organizer -> Projects -> Select Your Project

Right Pane shows the name of folder and also delete button at the right side allows you to delete all derived data contents.

Clear Simulator Cache;

Menu Bar -> iOS Simulator -> Reset Contents And Settings

Huseyin
  • 298
  • 2
  • 7
1

As I was explaining in a comment under the validated answer:

I was testing adding and removing calendar subscriptions. On a real device, you can remove a calendar subscription in Settings, Accounts but this menu does not exist in iOS Simulator and I did not want to reset the whole simulator.

So I ended up locally versioning my Device folder with git and perform the following commands to remove a calendar subscription after I added it:

$ git reset HEAD --hard
$ git clean -f

So the steps are:

  1. Install your application on the iOS Simulator and do what you have to do
  2. Identify your device in ~/Library/Developer/CoreSimulator/Devices/ and do a cd to it, then git init to create a git repository
  3. Once you want to save the state, perform git commit -a "Message"
  4. Do whatever changes the settings (ex: adding a calendar subscription) and perform your tests
  5. Shutdown the simulator
  6. Do git reset --hard HEAD
  7. Start the simulator, all changes done after git commit are gone.
Community
  • 1
  • 1
Thibault D.
  • 10,041
  • 3
  • 25
  • 56
1

In XCode, go to the Window menu option, select Devices and then you can just delete the ones you no longer need.

Simon
  • 11
  • 1
0

For Xcode <= 5

I added the following to my ~/.bash_profile

alias cleansim='rm -r ~/Library/Application\ Support/iPhone\ Simulator/5.1/Applications/*'

It just nukes all of the apps on the sim.

wfbarksdale
  • 7,498
  • 15
  • 65
  • 88
0

Using Xcode 14.1 and an iphone14 simulator I deleted the app as I would on an iPhone. This deletes all data from the app. When I rebuilt, and all the data in the Documents folder was removed.

tommmm
  • 182
  • 1
  • 4
0

Answer for Xcode 14 in Mac OS Ventura 13

Open your simulator. You can do in your terminal: open -a simulator

Then just Device -> Erase All Content and Settings...

enter image description here

Iván Yoed
  • 3,878
  • 31
  • 44