A notice to city hall that you are leaving the city or Japan.
Before you move to another city or leave the country.
When you leave Japan for good, it ends your health insurance and 国民年金 and sets the residence tax that remains.
General explanation, for guidance. Rules and amounts change: confirm with the responsible office.