This paper solves a second-best problem where a government has to choose whether to tax financial inflows (capital controls) or not, and when. A multi-stage optimal control technique is used to this end. First, it is shown that it is optimal to switch in finite time from capital controls to full financial liberalization whenever a measure of total wealth is above a certain threshold. In particular, a too large initial debt makes financial liberalization sub-optimal. Second, capital controls should be used countercyclically. Third, financial liberalization is not unaffordable only for poor countries, even wealthy countries might find it optimal to implement capital controls if they aim to keep a large amount of public expenditure.