If the country was defeated in war, the bondholders often lose all their money. Sometimes, though, the conqueror has its own reasons to want the debt to be repaid. If the country came to an end through peaceful negotiation, the same negotiation would decide who becomes responsible for its debts.
So, at the end of the U.S. Civil War, the United States declared that no one who had lent the Confederacy money to fight them was ever going to get any of it back. It even amended the Constitution (Amendment XIV, Section 4) to say that
[N]either the United States nor any State shall assume or pay any debt or obligation incurred in aid of insurrection or rebellion against the United States, or any claim for the loss or emancipation of any slave; but all such debts, obligations and claims shall be held illegal and void.
A clearer example of what you’re asking was Britain repudiating the debt of the republics it annexed in the Boer War: the republics had existed, they stopped existing, and the winner of the war decided that their debts were not its debts.
More often, there’s one or more successor states that sign a treaty saying who is responsible for paying the former country’s debts. An unusual example was when Germany was partitioned into the Federal Republic of Germany (West Germany) and the German Democratic Republic (East Germany) after World War II, the FRG declared that it wanted to repay the former German governments’ debts, in order to rehabilitate Germany’s reputation. The United States and its allies wanted to strengthen the FRG’s claim to be the legitimate government of Germany, but did not want it to be crippled by debt as the Weimar Republic had been. They signed a treaty in 1953 by which other countries would forgive some of the debts Germany owed them before the war, West Germany would pay most of the rest, and East Germany’s share would be repaid whenever Germany reunified (which happened in 1990). Japan, however, had already defaulted on its foreign debts in 1942, and its new government defaulted on its domestic debts in 1946.
Countries that break apart peacefully agree on how to split the debt. One country that did not break apart peacefully was the Russian Empire in 1917. In February 1918, the new Soviet Union repudiated the debts of both the Tsarist and Provisional governments, and in the early 1920s the Soviet Union signed bilateral treaties with Lithuania, Latvia, Estonia, Poland, Turkey and Persia agreeing that none of them would hold each other responsible for any of it—but France paid the interest on Russian bonds itself for the remainder of the war, and counted this sum as an additional loan to the defunct Russian Empire. These bonds continued to be traded until 1997, when, after the fall of the Soviet Union, the Russian Federation agreed to pay France $400M to settle all outstanding debt from before May 1945. This was 1% of the amount France claimed its investors were owed. Some of Russia’s other erstwhile allies did something similar, with Britain accepting compensation of 1.6% of face value in 1986. Finland, however, which became independent from Russia at the same time, was the only country in the world to continue paying its debts to the United States from WWI through the Great Depression.
In contrast, when the Soviet Union dissolved, the newly-independent republics negotiated its debts with the Group of Seven, consisting of the United States, the largest nations in Western Europe, and Japan (page 351). They first attempted to make all the successor states “jointly and severally” responsible for the Soviet debt, then realized that none of the smaller republics would ever be able to repay the debt if Russia defaulted and renegotiated that Russia would inherit all the Soviet Union’s debt along with all of its overseas assets, and finally, agreed in December 1991 that the G7 would reschedule the debt and Russia would pass economic reforms.
Most efforts to buy up old assets that a country defaulted on or seized and enlist another country to force someone to compensate you for it are not even as successful as the 1% settlement that France got from Russia in 1997. During the COVID-19 pandemic, a group of speculators bought up a claimed $1.6 trillion in debt that the People’s Republic of China had issued before the Chinese revolution, and unsuccessfully lobbied to have the U.S. government try to force the People’s Republic of China to repay it, somehow. (Linking does not imply endorsement.) The least important of the reasons that will never happen is that the Republic of China still believes that it exists, on Taiwan, whereas the People’s Republic of China believes that it is the successor state of the Republic of China, which is wrong to believe that it exists. Neither of them believes that it owes the owners of $1.6 trillion of the Republic of China’s old debts any money.
Countries that merge also agree on who will repay the debts of both. One interesting example is Texas becoming a state in 1845. The new State of Texas officially assumed the debts of the former Republic of Texas, but whether the federal government would bail Texas out and the debts would actually be repaid in full was not resolved until 1855. The debts of the formerly-independent realms that were annexed by Prussia in the 1860s were handled differently: while Prussia took over the debts of states it conquered in its wars of unification, and even agreed to accept a share of the Danish national debt along with the territory of Schleswig-Holstein, the Duchy of Brunswick, which negotiated its terms of unification, retained control over its own finances and repaid the money it had borrowed to build its railways through privatization. In 1870, Prussia both took Alsace-Lorraine from France and made France pay them a large sum of money.
In pragmatic terms, governments want to avoid defaulting on their domestic debts because that would harm their own people and economy, and on their foreign debts because that would damage their foreign relations and make it harder to borrow money again. These same considerations usually apply to whatever new government rules the same territory. So, in another régime change (which didn’t really bring the end of a nation, but does demonstrate the point), Daniel Ortega in 1979 announced that his new Socialist government of Nicaragua would not recognize the debts of the dictatorship he had overthrown, but the Cubans convinced him that it would be unwise to antagonize the Capitalist countries that way, and he reversed course..
Today, governments that are seriously considering default as an option tend to also be in the position of needing to borrow more money, and often negotiate rescheduling through such organizations as the International Monetary Fund and the Club de Paris, which consists of 22 countries and describes itself as, “an informal group of official creditors whose role is to find coordinated and sustainable solutions to the payment difficulties experienced by debtor countries.” Most of their debt-forgiveness programs, such as the Heavily-Indebted Poor Countries Initiative of 1996, ignore how the debts were acquired and focus on how much they are holding a country’s economy back.
On the Other Hand
Although the above is a very Realist perspective on international relations, others argue that there is or should be a system of international law based on rules, precedents and principles of justice. One international treaty that proposes to establish some formal rules for what happens in the absence of any other specific agreement is the 1983 Vienna Convention on Succession of States in respect of State Property, Archives and Debts—but this treaty has no legal weight because it is not in force, and only a handful of countries has signed it.
Another frequently-cited concept is “odious debt” developed by Alexandre Sachs, which derives a principle from examples like the ones I gave above, and argues that this should be applied consistently as a normative international law. Sachs argued that some debts need not be repaid because they are morally odious:
If a despotic power contracts debt, not for the needs and interest of the State, but to strengthen its despotic regime, to oppress the population that combats it, that debt is odious for the whole State. The debt need not be recognised by the Nation[....]
Other reasons that have been given in the past for a nation’s debt to be considered “odious” include: that the lender knew that the loan was exploitative, that the government that borrowed it had no authority to do so because it was illegitimate, that foreign rulers forced them to borrow it and used the money against the indigenous people now being asked to repay it, that a dictator borrowed in the name of his subjects but looted the money for himself, and that it was imposed under duress by an unfair treaty.
This has never been codified as a principle of international law, but there have been a number of international treaties that have accepted some of these as valid reasons to repudiate a debt, such as the the United States forcing Spain to accept the debt of the former Spanish Captaincy-General of Cuba in 1898, rather than the U.S. occupation government at the time or the new Republic of Cuba, on the grounds that these funds had been diverted by Spain and not used for the benefit of the Cuban people (Thanks for correcting me, @Silverfish!); Russia’s treaties of the 1920s and 1990s; and the Treaty of Versailles nullifying the debts of German colonies in Poland and Africa that had been used to transfer land to German settlers.
Note that there are organizations arguing that their interpretation of this principle should be applied universally to all nations,, leading to the emergence of a system more just, principled and consistent than the one we have now. Other authors see it the opposite way: “Despite international law, the concept of odious debt has been successfully used as a post hoc rationale when the victors of such conflicts are powerful enough to enforce their will on world financial markets and international lenders.”
There have been a much larger number of unilateral sovereign defaults where a government has made a moral argument that part of its debt is illegitimate, regardless of what anyone else thinks. And, if you will forgive me my relapse, also numerous cases where countries didn’t bother with any justification other than that it was in their own people’s best interest, where countries said they were paying a debt purely for practical reasons even though they felt no moral obligation to, and where one country’s debts were not forgiven even though it would seem to have as strong a case as another whose debts were.