Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@armcknight

This comment has been minimized.

Copy link

commented Sep 27, 2014

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 "{}"?

@ZevEisenberg

This comment has been minimized.

Copy link
Owner Author

commented Sep 27, 2014

Nice. I couldn’t figure out how to do it because I have not the shell fu.

@RileyMills

This comment has been minimized.

Copy link

commented Oct 7, 2014

Man, I just spent an hour or two putting something like this together. This is so much simpler.

@mattneub

This comment has been minimized.

Copy link

commented Jan 28, 2015

sixstringtheory I'd like to quote this in my book; may I?

@ArtSabintsev

This comment has been minimized.

Copy link

commented Jan 29, 2015

This is awesome - thanks, @ZevEisenberg and @sixstringtheory

@vittoriom

This comment has been minimized.

Copy link

commented Aug 20, 2015

I think it would be even better to pipe another grep like grep -v 'unavailable' after the first one. Otherwise the output will contain also unavailable devices that doesn't make sense to reset

@capnslipp

This comment has been minimized.

Copy link

commented Oct 21, 2015

That's so f'ing handy.

About the only way I can think of improving this is to make a wrapper simctl that normally calls the real …/Xcode.app/…/simctl, but when given simctl erase --all calls your code instead. ;-)

So… much cheers.

@Blackjacx

This comment has been minimized.

Copy link

commented Nov 10, 2015

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

This comment has been minimized.

Copy link
Owner Author

commented Dec 16, 2015

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

@stanislaw

This comment has been minimized.

Copy link

commented Aug 26, 2016

Very useful, thanks!

@jeanmw

This comment has been minimized.

Copy link

commented May 16, 2017

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

@neilt

This comment has been minimized.

Copy link

commented Sep 25, 2017

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

@SupriyaKalghatgi

This comment has been minimized.

Copy link

commented Nov 13, 2017

Thank you so much
it worked

@sampurcell93

This comment has been minimized.

Copy link

commented Dec 20, 2017

the amount of disk this freed up is astounding, thanks

@armcknight

This comment has been minimized.

Copy link

commented May 15, 2018

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

@consbulaquena

This comment has been minimized.

Copy link

commented Jul 25, 2018

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

xcrun simctl erase all

@CyberToro

This comment has been minimized.

Copy link

commented Nov 30, 2018

This is amazing! Thanks a lot!

@ashermes

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented Apr 3, 2019

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

This comment has been minimized.

Copy link

commented Apr 3, 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.

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 "{}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.