Peninsular War: French troops engage British-Portuguese forces in the Battle of Majadahonda.

The Battle of Majadahonda (11 August 1812) saw an Imperial French cavalry division led by Anne-Franois-Charles Trelliard attack two brigades of cavalry under Benjamin d'Urban and forming the advance guard of Arthur Wellesley, Earl of Wellington's army. Trelliard's leading brigade routed d'Urban's Portuguese horsemen and overran three British cannons. King's German Legion (KGL) cavalry led by Eberhardt Otto George von Bock intervened to halt the Imperial French horsemen, but were finally compelled to withdraw when Trelliard committed his second and third brigades to the contest. The Imperial French cavalry was unable to cope with a KGL infantry battalion defending a village and they withdrew at the approach of additional British cavalry and infantry. This Peninsular War action was fought near Majadahonda, which is located 16 kilometres (9.9 mi) northwest of Madrid.

The Peninsular War (1807–1814) was the military conflict fought in the Iberian Peninsula by Spain, Portugal, and the United Kingdom against the invading and occupying forces of the First French Empire during the Napoleonic Wars. In Spain, it is considered to overlap with the Spanish War of Independence. The war began when the French and Spanish armies invaded and occupied Portugal in 1807 by transiting through Spain, and it escalated in 1808 after Napoleonic France had occupied Spain, which had been its ally. Napoleon Bonaparte forced the abdications of Ferdinand VII and his father Charles IV and then installed his brother Joseph Bonaparte on the Spanish throne and promulgated the Bayonne Constitution. Most Spaniards rejected French rule and fought a bloody war to oust them. The war on the peninsula lasted until the Sixth Coalition defeated Napoleon in 1814, and it is regarded as one of the first wars of national liberation and is significant for the emergence of large-scale guerrilla warfare.

The war began in Spain with the Dos de Mayo Uprising on 2 May 1808 and ended on 17 April 1814 with the restoration of Ferdinand VII to the monarchy. The French occupation destroyed the Spanish administration, which fragmented into quarrelling provincial juntas. The episode remains as the bloodiest event in Spain's modern history, doubling in relative terms the Spanish Civil War.A reconstituted national government, the Cortes of Cádiz—in effect a government-in-exile—fortified itself in the secure port of Cádiz in 1810, but could not raise effective armies because it was besieged by 70,000 French troops. British and Portuguese forces eventually secured Portugal, using it as a safe position from which to launch campaigns against the French army and provide whatever supplies they could get to the Spanish, while the Spanish armies and guerrillas tied down vast numbers of Napoleon's troops. By restricting French control of territory, the combined allied forces, both regular and irregular, prevented Napoleon's marshals from subduing the rebellious Spanish provinces, and the war continued through years of stalemate.The British Army, under then Lt. Gen. Sir Arthur Wellesley, later the 1st Duke of Wellington, guarded Portugal and campaigned against the French in Spain alongside the reformed Portuguese army. The demoralized Portuguese army was reorganized and refitted under the command of Gen. William Beresford, who had been appointed commander-in-chief of the Portuguese forces by the exiled Portuguese royal family, and fought as part of the combined Anglo-Portuguese Army under Wellesley.

In 1812, when Napoleon set out with a massive army on what proved to be a disastrous French invasion of Russia, a combined allied army under Wellesley pushed into Spain, defeating the French at Salamanca and taking the capital Madrid. In the following year Wellesley scored a decisive victory over King Joseph Bonaparte's army in the Battle of Vitoria. Pursued by the armies of Britain, Spain and Portugal, Marshal Jean-de-Dieu Soult, no longer getting sufficient support from a depleted France, led the exhausted and demoralized French forces in a fighting withdrawal across the Pyrenees during the winter of 1813–1814.

The years of fighting in Spain were a heavy burden on France's Grande Armée. While the French were victorious in battle, they were eventually defeated, as their communications and supplies were severely tested and their units were frequently isolated, harassed or overwhelmed by partisans fighting an intense guerrilla war of raids and ambushes. The Spanish armies were repeatedly beaten and driven to the peripheries, but they would regroup and relentlessly hound and demoralize the French troops. This drain on French resources led Napoleon, who had unwittingly provoked a total war, to call the conflict the "Spanish Ulcer".War and revolution against Napoleon's occupation led to the Spanish Constitution of 1812, promulgated by the Cortes of Cádiz, later a cornerstone of European liberalism. The burden of war destroyed the social and economic fabric of Portugal and Spain, and ushered in an era of social turbulence, increased political instability, and economic stagnation. Devastating civil wars between liberal and absolutist factions, led by officers trained in the Peninsular War, persisted in Iberia until 1850. The cumulative crises and disruptions of invasion, revolution and restoration led to the independence of most of Spain's American colonies and the independence of Brazil, which remained a monarchy, after severing ties with Portugal.