In June 1808, following the widespread uprisings against the French occupation of Spain, Napoleon organized French units into flying columns to pacify Spain's major centres of resistance. One of these, under General Dupont, was dispatched across the Sierra Morena and south through Andalusia to the port of Cádiz where a French naval squadron lay at the mercy of the Spanish. The Emperor was confident that with 20,000 men, Dupont would crush any opposition encountered on the way. Events proved otherwise, and after storming and plundering Córdoba in July, Dupont retraced his steps to the north of the province to await reinforcements. Meanwhile, General Castaños, commanding the Spanish field army at San Roque, and General von Reding, Governor of Málaga, travelled to Seville to negotiate with the Seville Junta—a patriotic assembly committed to resisting the French incursions—and to turn the province's combined forces against the French.
Dupont's failure to leave Andalusia proved disastrous. Between 16 and 19 July, Spanish forces converged on the French positions stretched out along villages on the Guadalquivir and attacked at several points, forcing the confused French defenders to shift their divisions this way and that. With Castaños pinning Dupont downstream at Andújar, Reding successfully forced the river at Mengibar and seized Bailén, interposing himself between the two wings of the French army. Caught between Castaños and Reding, Dupont attempted vainly to break through the Spanish line at Bailén in three bloody and desperate charges, losing more than 2,500 men.
His attacks defeated, Dupont called for an armistice and was compelled to sign the Convention of Andújar which stipulated the surrender of almost 18,000 men, making Bailén the worst disaster and capitulation of the Peninsular War. In one of the most controversial episodes of the campaign, Dupont ordered his subordinate, Dominique Honoré Antoine Vedel, to surrender his division. Though Vedel's troops were outside the Spanish encirclement, with a good chance of escape, that general surrendered.
When news of the catastrophe reached the French high command in Madrid, the result was a general retreat to the Ebro, abandoning much of Spain to the insurgents. France's enemies in Spain and throughout Europe cheered at this first check to the hitherto unbeatable Imperial armies—tales of Spanish heroism inspired Austria and showed the force of nationwide resistance to Napoleon, setting in motion the rise of the Fifth Coalition against France.
Alarmed by these developments, Napoleon briefly took command of the Spanish theatre and, at the head of fresh troops and overwhelming numbers, dealt devastating blows to the vacillating Spanish rebels and their British allies, recapturing Madrid in November 1808. In doing so, however, the French military committed enormous resources to a long war of attrition characterized by heavy losses to the implacable Spanish guerrillas, ultimately leading to the expulsion of French armies from Spain and the exposure of southern France to invasion in 1814 by combined Spanish, British, and Portuguese forces.