Magnetic thermal switch for heat management at the nanoscale