The Anglo-Spanish War between 1796 and 1802, and again from 1804 to 1808, was a part of the French Revolutionary Wars and the Napoleonic Wars. The war ended in an alliance.
In the War of the First Coalition, Spain had fought against Revolutionary France and had been defeated. In 1796, Spanish Prime Minister Godoy faced a decision: whether to continue to fight Revolutionary France or to join the French side and hope for better times. He chose the latter and signed the Treaty of San Ildefonso in 1796, allying Spain to France, in exchange for French support for Charles IV's relations ruling the Italian Duchy of Parma.
In response, the British blockaded Spain in 1797, impairing communications with its American colonies. By the end of 1798, a Spanish battle fleet was defeated in the Battle of Cape St Vincent, and Minorca and Trinidad were occupied by the British. A British attempt to capture the strategically important island of Tenerife failed.
The Treaty of Amiens in 1802 provided for a temporary truce in hostilities, only to be broken in 1804, when, as part of the reopening of the war with Napoleons France, the British captured a Spanish squadron of frigates carrying gold bullion to Cádiz. The French planned an invasion of England in the coming year; the Spanish fleet was to be an integral part in assisting this invasion. At the Battle of Trafalgar, in 1805, a combined Franco-Spanish fleet, attempting to join forces with the French fleets in the north for the invasion, were attacked by a British fleet in one of history's greatest naval engagements. The decisive British victory ended Napoleon's hopes of an invasion of Britain . It seriously shook the resolve of the unpopular Spanish government, which began to doubt the utility of its uncertain alliance with France. Meanwhile, a British campaign (1806–1807) to conquer the strategically important Rio de la Plata region in South America met with failure.
Godoy withdrew from the Continental System that Napoleon had devised to combat Britain, only to join it again in 1807, after Napoleon had defeated the Prussians. Napoleon, however, had lost his faith in Godoy and King Charles; there was also growing support in Spain for the king's son, Ferdinand, who opposed the widely despised Godoy. Ferdinand, however, favoured an alliance with Britain, and Napoleon had always doubted the trustworthiness of any Bourbon royalty.
In 1807, France and Spain invaded Portugal, and, on 1 December, Lisbon was captured with no military opposition. In the beginning of 1808, the French presence in Spain was so predominant that it led to revolt. Napoleon then removed King Charles and his son Ferdinand to Bayonne and forced them both to abdicate on 5 May, giving the throne to his brother Joseph. This led to the Peninsular War and the de facto end of the Anglo-Spanish War, as George Canning, foreign secretary of His Majesty's Government, declared:
"No longer remember that war has existed between Spain and Great Britain. Every nation which resists the exorbitant power of France becomes immediately, and whatever may have been its previous relations with us, the natural ally of Great Britain."