When is it optimal for a government to default on its legal repayment obligations? We answer this question for a small open economy with domestic production risk in which contracting frictions make it optimal for the government to finance itself by issuing non-contingent debt. We show that Ramsey optimal policies occasionally deviate from the legal repayment obligation and repay debt only partially, even if such deviations give rise to significant 'default costs'. Optimal default improves the international diversification of domestic output risk, increases the efficiency of domestic investment and - for a wide range of default costs - significantly increases welfare relative to a situation where default is simply ruled out from Ramsey optimal plans. We show analytically that default is optimal following adverse shocks to domestic output, especially for very negative international wealth positions. A quantitative analysis reveals that for empirically plausible wealth levels, default is optimal only in response to disaster-like shocks to domestic output, and that following such shocks default can be Ramsey optimal even if the net foreign asset position is positive.