     delete @deviceProcesses[uuid]
     callback null, uuid
 
+  removeDeletedDeviceDirectory: (device, callback) =>
+    fs.remove @getDevicePath(device), (error) =>
+      console.error error if error?
+      callback()
+
   stopDevices: (callback=->) =>
     async.each @runningDevices, @stopDevice, callback
 

  refreshDevices: (devices, callback) =>
    debug 'refreshDevices', _.pluck(devices, 'uuid')

    @getDevicesByOperation devices, (devicesToStart, devicesToStop, devicesToRestart, unchangedDevices) =>
      connectorsToInstall = _.uniq _.pluck devicesToStart, 'connector'

      async.series(
        [
          (callback) => async.each connectorsToInstall, @installConnector, callback
          (callback) => async.each devicesToStop, @stopDevice, callback
          (callback) => async.each devicesToStart, @setupDevice, callback
          (callback) => async.each devicesToStart, @startDevice, callback
          (callback) => async.each devicesToRestart, @restartDevice, callback
        ]
        (error, result)=>
          @runningDevices = _.union devicesToStart, devicesToRestart
          @emit 'update', _.union(devicesToStart, devicesToRestart, devicesToStop, unchangedDevices)
          callback error, result
      )

  getDevicesByOperation: (newDevices=[], callback=->) =>
    oldDevices = _.clone @runningDevices
    debug 'getDevicesByOperation'
    debug 'newDevices', _.pluck(newDevices, 'name')
    debug 'oldDevices', _.pluck(oldDevices, 'name')

    devicesToProcess = _.clone newDevices
    async.map devicesToProcess, @deviceExists, (error, remainingDevices) =>
      return callback error if error?

      remainingDevices = _.compact remainingDevices
      debug 'devices that exist', _.pluck(remainingDevices, 'name')

      devicesToStop = _.filter remainingDevices, stop: true
      debug 'devicesToStop:', _.pluck(devicesToStop, 'name')
      remainingDevices = _.difference remainingDevices, devicesToStop

      devicesToStart = _.reject remainingDevices, (device) =>
        _.findWhere oldDevices, uuid: device.uuid, token: device.token

      debug 'devicesToStart:', _.pluck(devicesToStart, 'name')

      remainingDevices = _.difference remainingDevices, devicesToStart

      devicesToRestart = _.filter remainingDevices, (device) =>
        deviceToRestart =_.findWhere oldDevices, uuid: device.uuid
        return deviceToRestart?.token != device.token

      debug 'devicesToRestart:', _.pluck(devicesToRestart, 'name')

      unchangedDevices = _.difference remainingDevices, devicesToRestart
      debug 'unchangedDevices', _.pluck(unchangedDevices, 'name')

      callback devicesToStart, devicesToStop, devicesToRestart, unchangedDevices

  deviceExists: (device, callback=->) =>
    debug 'deviceExists', device.uuid

      silent: true
      options: []
      cwd: devicePath
      logFile: path.join devicePath, 'forever.log'
      outFile: path.join devicePath, 'forever.stdout'
      errFile: path.join devicePath, 'forever.stderr'
      command: 'node'
      checkFile: false

