1806–1898

The first major territory Spain was to lose in the nineteenth century was the vast and wild Louisiana Territory, which stretched north to Canada and was ceded by France in 1763. The French, under Napoleon, took back possession as part of the Treaty of San Ildefonso in 1800 and sold it to the United States (Louisiana Purchase, 1803).

The destruction of the main Spanish fleet, under French command, at the Battle of Trafalgar (1805) undermined Spain's ability to defend and hold on to its empire. The later intrusion of Napoleonic forces into Spain in 1808 (see Peninsular War) cut off effective connection with the empire. But it was internal tensions that ultimately ended the empire in the Americas.

Napoleon's sale in 1803 of the Louisiana Territory to the United States was to cause border disputes between the United States and Spain that, with rebellions in West Florida (1810) and in the remainder of Louisiana at the mouth of the Mississippi, led to their eventual cession to the United States, along with the sale of all of Florida, in the Adams–Onís Treaty (1819).

In 1808 the Spanish king was tricked and Spain was taken over by Napoleon without firing a shot, but the brutal French provoked a popular uprising from the Spanish people and the grinding guerrilla warfare, which Napoleon dubbed his "ulcer", the Peninsular War, (famously depicted by the painter Goya) ensued, followed by a power vacuum lasting up to a decade and turmoil for several decades, civil wars on succession disputes, a republic, and finally a liberal democracy. Spain lost all the colonial possessions in the first third of the century, except for Cuba, Puerto Rico and, isolated on the far side of the globe, the Philippines, Guam and nearby Pacific islands, as well as Spanish Sahara, parts of Morocco, and Spanish Guinea.

The wars of independence in the Americas were triggered by another failed British attempt to seize Spanish American territory, this time in the Viceroyalty of the Río de la Plata in 1806. The viceroy retreated hastily to the hills when defeated by a small British force. However when the Criollos militias and colonial army thrashed the now reinforced British force in 1807 and, with the example of the North American revolutionaries very much in their minds, they quickly set about the business of winning their own independence and inspiring independence movements elsewhere in the Americas. A long period of wars began which led to the independence of Paraguay (1811) and Uruguay (1815 but subsequently ruled by Brazil until 1828). José de San Martín campaigned for freedom in Argentina (1816), Chile (1818) and Peru (1821). Further north Simon Bolivar led forces that won independence for the area that is currently Venezuela, Colombia (included Panama until 1903), Ecuador, and Bolivia by 1825. In 1810 a free thinking priest, Miguel Hidalgo y Costilla, declared Mexican independence, which was won by 1821. Central America declared its independence in 1821 and was joined to Mexico for a brief time (1822–23). Santo Domingo likewise declared independence in 1821 and began negotiating for inclusion in Bolivar's Republic of Gran Colombia, but was quickly occupied by Haiti, which ruled it until an 1844 revolution. Thus only Cuba and Puerto Rico remained in Spanish hands in the New World.

In devastated Spain the post-Napoleonic era created a political vacuum, broke apart any traditional consensus on sovereignty, fragmented the country politically and regionally and unleashed wars and disputes between progressives, liberals and conservatives. The instability inhibited Spain's development, which had started fitfully gathering pace in the previous century. A brief period of improvement occurred in the 1870s when the capable Alfonso XII of Spain and his thoughtful ministers succeeded in restoring some vigour to Spanish politics and prestige, but this was cut short by Alfonso's early death.
An increasing level of nationalist, anti-colonial uprisings in various colonies culminated with the Spanish–American War of 1898, fought primarily over Cuba. Military defeat was followed by the independence of Cuba and the cession, for US$20 million, of Puerto Rico, the Philippines, and Guam to the United States. Her American presence ended, Spain then sold her Pacific Ocean possessions to Germany in 1899, retaining only her African territories.