Pillar 3a assets can be withdrawn regardless of your nationality and which country you move to. The rules are different for the second pillar.
Tax optimization is only relevant if you are not moving to a country with a elevant double-taxation agreement with Switzerland. If you move to a country with a relevant DTA, you can reclaim the Swiss withholding tax by proving that you tax resident in that country.
If you move to a country which does not have a relevant DTA, then transferring your pillar 3a assets to a retirement foundation in a low-tax canton ahead of withdrawing them can be a good move. However, for 40,000 francs of pillar 3a assets, the tax differences may be negligible. Schwyz is generally the most favorable canton for all pillar 3a asset brackets.
An important consideration is whether your current pillar 3a foundation charges fees for transferring assets to a different foundation. Many do, and the fees may be so high as to nullify the tax advantage of transferring to a different canton. Some foundations charge fees for irregular withdrawals ahead of retirement age, so this is also important to look at.
You can find some of the fees charged on the detailed product information pages here: