Skip to content

Instantly share code, notes, and snippets.

@jfro
Created November 3, 2014 21:41
Show Gist options
  • Save jfro/8dc856e325184012c808 to your computer and use it in GitHub Desktop.
Save jfro/8dc856e325184012c808 to your computer and use it in GitHub Desktop.
Quick script to remove stale simulator device folders unused by Xcode, lists invalid devices if run without flags
#!/usr/bin/ruby
require 'fileutils'
require 'optparse'
xcodePath = %x{xcode-select -print-path}
puts "Checking simulator devices against Xcode: #{xcodePath}"
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: clean-sims.rb [options]"
opts.on("-d", "--delete", "Delete stale simulator devices") do |d|
options[:delete] = d
end
end.parse!
# Ask Xcode for sim device IDs
activeSims = %x{xcrun simctl list}
validDeviceIDs = {}
for line in activeSims.split("\n")
m = /(.*)\s\(([\w\d]{8}-[\w\d]{4}-[\w\d]{4}-[\w\d]{4}-[\w\d]{12})/m.match(line)
if m
validDeviceIDs[m[2]] = m[1].strip
end
end
def getDeviceInfo(devicePath, key)
return %x{/usr/libexec/PlistBuddy -c "Print :#{key}" #{devicePath}/device.plist}.strip
end
# find all on-disk devices, compare against Xcode's list
for deviceFolder in Dir.glob(File.join(Dir.home, "/Library/Developer/CoreSimulator/Devices/*"))
deviceID = File.basename(deviceFolder)
isValid = false
if not validDeviceIDs.has_key? deviceID
name = getDeviceInfo(deviceFolder, 'name')
runtime = getDeviceInfo(deviceFolder, 'runtime')
puts "#{deviceID}(#{name}, #{runtime}) is invalid"
if options[:delete]
FileUtils.remove_entry_secure(deviceFolder)
end
end
end
@scriptease
Copy link

I changed the regex to this to remove all uninstalled simulators too:

(.*)\s\(([\w\d]{8}-[\w\d]{4}-[\w\d]{4}-[\w\d]{4}-[\w\d]{12})(?:(?!unavailable).)*$

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