Spain in the 19th century was a country in turmoil. Under Napoleonic Spain from 1808 to 1814, the massively destructive "Peninsular War" ensued. Following the Spanish Constitution of 1812, Spain was divided between the constitution's liberal principles and the absolutism personified by the rule of Ferdinand VII, who repealed the 1812 Constitution for the first time in 1814, only to be forced to swear over the constitution again in 1820 after a liberal pronunciamiento, giving way to the brief Trienio Liberal (1820–1823). This brief period came to an abrupt end with Ferdinand again abolishing the 1812 constitution and the start of the Ominous Decade (1823–1833) of absolutist rule for the last ten years of his reign.

Economic transformations throughout the century included the privatisation of communal municipal lands—not interrupted but actually intensified and legitimised during the Fernandine absolutist restorations —as well as the confiscation of Church properties. The early century saw the loss of the bulk of the Spanish colonies in the New World in the 1810s and 1820s, except for Cuba and Puerto Rico.

The regency of Maria Christina and the reign of Isabella II brought reforms repealing the extremes of the absolutist Ominous Decade. Civil wars broke out in the country—the so-called Carlist Wars—pitting the government forces against the reactionary Carlists, a legitimist movement in favour of the ancien régime. Disaffection with Isabella's government and high disapproval from many people led to repeated military intervention in political affairs and to several revolutionary attempts against the government, including the 1854 revolution. The 1868 Glorious revolution deposed Isabella and installed a provisional government, leading up to the election of a constituent assembly under universal manhood suffrage that elaborated the 1869 constitution of the Kingdom of Spain. The brief spell of Amadeo of Savoy as constitutional monarch was followed after his abdication by the proclamation of the First Spanish Republic, which was replaced after a 1874 coup by the reign of Alfonso XII, bringing the Bourbon dynasty back to power.

Abdications of Charles IV and Ferdinand VII (1808)

The reign of Charles IV was characterized by his lack of interest in governing. His wife Maria Luisa dominated him, and both husband and wife backed Manuel de Godoy as first minister. Many of Godoy's decisions were criticised and increasingly Charles's son and heir, Ferdinand gathered support against his detested father. A mob supporting Ferdinand attacked Godoy at the palace of Aranjuez, arresting him. Charles IV under pressure abdicated in favor of his son, now Ferdinand VII. Napoleon had already invaded Portugal 1807, and had troops on the ground in Spain already. Napoleon called on Ferdinand to come to Bayonne, where he was currently situated. Ferdinand went, expecting Napoleon to affirm his status as king of Spain. Unbeknownst to Ferdinand, Napoleon also summoned Charles IV. Napoleon called on Ferdinand to abdicate in favor of his father, who had abdicated under pressure. Charles did not want to leave a path open for his detested son to be his heir, and abdicated in favor of Napoleon himself. Napoleon at this point designated his older brother, Joseph Bonaparte as king of Spain. An assembly of Spaniards ratified the Bayonne Constitution, Spain's first, which Joseph I signed. Although the constitution never came into full force, the fact that it provided for representation from regions of Spain and elsewhere in the Spanish Empire, namely Spanish America and the Philippines, set an important precedent.

Napoleon's 1808 invasion and Spanish resistance, (1808–1814)

Although there were a few Spaniards who supported Napoleon's seizure of power in Spain, many regional centers rose up and formed juntas to rule in the name of the ousted Bourbon king, Ferdinand VII. Spanish America also created juntas to rule in the name of the king, since Joseph I was considered an illegitimate sovereign. Bloody warfare raged in Spain and Portugal in the Peninsular War, much of which was fought using guerrilla tactics.

Spain's first national assembly (1810–1814)

The Cortes of Cádiz was the first national assembly to claim sovereignty in Spain and the Spanish Empire. It represented the abolition of the old kingdoms and the recognition of overseas components of the Spanish Empire for representation. The opening session was held on 24 September 1810.

In November 1809, the army of the Central Junta was routed at the Battle of Ocaña. French forces took control of southern Spain and the Junta retreated to Cádiz. Cádiz was besieged by the French from 5 February 1810 to 24 August 1812 but never captured. The Central Junta dissolved itself on 29 January 1810, and set up a five-person Regency. The five regents then convened the meeting of the "Cortes of Cádiz", operating as a government in exile.

The delegates to the Cortes were to be representatives of the provinces and colonies, but the Regency was unable to hold elections in much of Spain or the Americas. The Regency therefore tried to establish interim territorial representation in the assembly, which approved a decree that it represented the Spanish nation, with sovereignty over Spain and Americas.

The Cortes opened its session in September 1810 on the Isle of Leon. The Cortes consisted of 97 deputies, of whom 47 were Cadiz residents serving as alternates.

The Spanish Constitution of 1812 was established on 19 March 1812 by the Cortes of Cádiz. It abolished the Inquisition and absolute monarchy, and established the principles of universal male suffrage, national sovereignty, constitutional monarchy, and freedom of the press, and supported land reform and free enterprise.

Reaction (1814–1820)

On 24 March 1814, six weeks after returning to Spain, Ferdinand VII abolished the constitution. King Ferdinand VII's refusal to agree to the liberal Spanish Constitution of 1812 on his accession to the throne in 1814 came as little surprise to most Spaniards; the king had signed on to agreements with the clergy, the church, and with the nobility in his country to return to the earlier state of affairs even before the fall of Napoleon. The decision to abrogate the Constitution was not welcomed by all, however. Liberals in Spain felt betrayed by the king whom they had decided to support, and many of the local juntas that had pronounced against the rule of Joseph Bonaparte lost confidence in the king's rule. The army, which had backed the pronouncements, had liberal leanings that made the king's position tenuous. Even so, agreements made at the Congress of Vienna (where Spain was represented by Pedro Gómez Labrador, Marquis of Labrador) starting a year later would cement international support for the old, absolutist regime in Spain.

The Spanish Empire in the New World had largely supported the cause of Ferdinand VII over the Bonapartist pretender to the throne in the midst of the Napoleonic Wars. Joseph I had promised radical reform, particularly the centralization of the state, which would cost the local authorities in the American empire their autonomy from Madrid. The Spanish Americans, however, did not support absolutism and wanted self-governance. The juntas in the Americas did not accept the governments of the Europeans, neither the French or Spaniards.

Trienio Liberal (1820–1823)

A conspiracy of liberal mid-ranking officers in the expedition being outfitted at Cádiz mutinied before they were shipped to the Americas. Led by Rafael del Riego, the conspirators seized their commander and led their army around Andalusia hoping to gather support; garrisons across Spain declared their support for the would-be revolutionaries. Riego and his co-conspirators demanded that the liberal Constitution of 1812 be restored. Before the coup became an outright revolution, King Ferdinand agreed to the demands of the revolutionaries and swore by the constitution. A "Progresista" (liberal) government was appointed, though the king expressed his disaffection with the new administration and constitution.

Three years of liberal rule (the Trienio Liberal) followed. The Progresista government reorganized Spain into 52 provinces, and intended to reduce the regional autonomy that had been a hallmark of Spanish bureaucracy since Habsburg rule in the 16th and 17th centuries. The opposition of the affected regions – in particular, Aragon, Navarre, and Catalonia – shared the king's antipathy for the liberal government. The anticlerical policies of the Progresista government led to friction with the Roman Catholic Church, and its attempts to bring about industrialization alienated ancient trade guilds. The Inquisition—which had been abolished by both Joseph Bonaparte and the Cortes of Cádiz during the French occupation—was ended again by the Progresista government, summoning up accusations of being nothing more than afrancesados (Francophiles), who only six years before had been forced out of the country. More radical liberals attempted to revolt against the entire idea of a monarchy, constitutional or otherwise, in 1821; these republicans were suppressed, though the incident served to illustrate the frail coalition that bound the Progresista government together.

The election of a radical liberal government in 1823 further destabilized Spain. The army – whose liberal leanings had brought the government to power – began to waver when the Spanish economy failed to improve, and in 1823, a mutiny in Madrid had to be suppressed. The Jesuits (who had been banned by Charles III in the 18th century, only to be rehabilitated by Ferdinand VII after his restoration) were banned again by the radical government. For the duration of liberal rule, King Ferdinand (though technically head of state) lived under virtual house arrest in Madrid.

The Congress of Vienna ending the Napoleonic Wars had inaugurated the "Congress system" as an instrument of international stability in Europe. While Ferdinand had been rebuffed by the "Holy Alliance" of Russia, Austria, and Prussia in his request for help against the liberal revolutionaries in 1820, by 1822 the "Concert of Europe" was sufficiently uneasy with Spain's liberal government and its surprising hardiness that they were prepared to intervene on Ferdinand's behalf. In 1822, the Congress of Verona authorized France to intervene. Louis XVIII – himself an arch-reactionary – was only too happy to put an end to Spain's liberal experiment, and a massive army – the "100,000 Sons of Saint Louis" – was dispatched across the Pyrenees in April 1823. The Spanish army, fraught by internal divisions, offered little resistance to the well organised French force, who seized Madrid and reinstalled Ferdinand as absolute monarch. The liberals' hopes for a new Spanish War of Independence were not fulfilled.

The "Ominous Decade" (1823–1833)

Immediately following the restoration of absolutist rule in Spain, King Ferdinand VII embarked on a policy intended to restore old conservative values to government; the Jesuit Order and the Spanish Inquisition were reinstated once more, and some autonomy was again devolved to the provinces of Aragon, Navarre, and Catalonia. Although he refused to accept the loss of the American colonies, Ferdinand was prevented from taking any further action against the rebels in the Americas by the opposition of the United Kingdom and the United States, who voiced their support of the new Latin American republics in the form of the Monroe Doctrine. The recent betrayal of the army demonstrated to the king that his own government and soldiers were untrustworthy, and the need for domestic stability proved to be more important than the reconquest of the Empire abroad. As a result, the destinies of Spain and her empire on the American mainland were to permanently take separate paths.