Skip to content

Instantly share code, notes, and snippets.

@ZevEisenberg
Last active November 30, 2022 09:27
Show Gist options
  • Save ZevEisenberg/5a172662cb576872d1ab to your computer and use it in GitHub Desktop.
Save ZevEisenberg/5a172662cb576872d1ab to your computer and use it in GitHub Desktop.
Reset all iOS simulators with this one weird trick
osascript -e 'tell application "iOS Simulator" to quit'
osascript -e 'tell application "Simulator" to quit'
xcrun simctl erase all
@Blackjacx
Copy link

Since Xcode 7 it is possible to do xcrun simctl erase all and combined with your osascript -e 'tell application "iOS Simulator" to quit' this is perfect for putting it inside the before integration trigger of a Xcode bot

@ZevEisenberg
Copy link
Author

Thanks, @Blackjacx! Updated with the new first-party method of doing this.

@stanislaw
Copy link

Very useful, thanks!

@jeanmw
Copy link

jeanmw commented May 16, 2017

Yay, thanks so much this is super useful/convenient for testing!

@neilt
Copy link

neilt commented Sep 25, 2017

In Xcode 9, you can use xcrun simctl shutdown all to shutdown the simulators.

@SupriyaKalghatgi
Copy link

Thank you so much
it worked

@sampurcell93
Copy link

the amount of disk this freed up is astounding, thanks

@armcknight
Copy link

hah, just found this again! like the updated version, so much cleaner!

@consbulaquena
Copy link

I thought there was a problem with the database, turns out the simulator only needs resetting!

xcrun simctl erase all

@lajosdeme
Copy link

This is amazing! Thanks a lot!

@ashermes
Copy link

ashermes commented Apr 1, 2019

Based on the answer above, I improved the command:

sudo killall -9 com.apple.CoreSimulator.CoreSimulatorService; xcrun simctl list devices | grep "iPhone\|iPad" | awk '{print $(NF -1)}' | cut -d "(" -f2 | cut -d ")" -f1 | xargs -I {} xcrun simctl erase "{}"

The first part (sudo killall) will make sure that the simulator is closed, so there won't be any simulator in Booted or Creating state (which will throw an error on delete).
I changed the UDID retrieval, as for devices like "iPad (5th generation) (D88F16C6-AEF4-40BF-A8A5-0CA2FB548A9D) (Shutdown)" it would pick up the incorrect text. In my version, it will always pick up the second to last word, instead of the 3rd word.

Also, when I get devices, I filter only iPhone and iPad simulators. This gives a bit of flexibility.

I use this combination as a hard reset for all my simulators.

@MemoryReload
Copy link

pretty good, but what about this one liner without the variable: xcrun simctl list devices | grep -v '^[-=]' | cut -d "(" -f2 | cut -d ")" -f1 | xargs -I {} xcrun simctl erase "{}"?

this grep-cut trick does not work fun now. :Q

@MemoryReload
Copy link

Based on the answer above, I improved the command:

sudo killall -9 com.apple.CoreSimulator.CoreSimulatorService; xcrun simctl list devices | grep "iPhone\|iPad" | awk '{print $(NF -1)}' | cut -d "(" -f2 | cut -d ")" -f1 | xargs -I {} xcrun simctl erase "{}"

The first part (sudo killall) will make sure that the simulator is closed, so there won't be any simulator in Booted or Creating state (which will throw an error on delete).
I changed the UDID retrieval, as for devices like "iPad (5th generation) (D88F16C6-AEF4-40BF-A8A5-0CA2FB548A9D) (Shutdown)" it would pick up the incorrect text. In my version, it will always pick up the second to last word, instead of the 3rd word.

Also, when I get devices, I filter only iPhone and iPad simulators. This gives a bit of flexibility.

I use this combination as a hard reset for all my simulators.

This also does not work fun, so I make a fix. : )

sudo killall -9 com.apple.CoreSimulator.CoreSimulatorService; xcrun simctl list devices | grep "iPhone\|iPad" | awk '{print $(NF -1)}' | cut -d "(" -f2 | cut -d ")" -f1 | grep -E "^[0-9A-F]{8}-([0-9A-F]{4}-){3}[0-9A-F]{12}$" | xargs -I {} xcrun simctl erase "{}"

@soffes
Copy link

soffes commented Nov 12, 2019

I found this while looking to delete all simulators (instead of erase). If you want to do that, you can delete them all with xcrun simctl delete all.

@zfogg
Copy link

zfogg commented Oct 19, 2020

none of this stuff worked for me

# run this multiple times
pkill -flai xcode

# if any $process_id refuses to die and keeps printing out when doing the previous command, run this
kill -9 $process_id

🥳

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment