I checked with my employer since my contract will end at the end of this year. The process is that you have to cancel your work permit first, and then cancel your visa at the immigration office. After that, they’ll give you a 7-day grace period to leave Thailand or prepare for a new work permit.
Note: your visa isn’t automatically cancelled just because you go abroad.