The contest in Spain, the ultimate reason for the war, seesawed back and forth. Fighting did not begin across the Pyrenees until 1704, the year that the Austrian claimant, Archduke Charles, reached Lisbon. An Anglo-Dutch naval expedition took Gibraltar on 3 August 1704 and then held off a French fleet at the battle of Velez-Málaga. In October 1705 the Allies took Barcelona after a siege that cut off the city from land and sea. Importantly, Leopold I died in 1705, to be succeeded by Joseph I. There was no reason to believe that Joseph I would not have a long reign, but should he die young, Archduke Charles would succeed him.
Philip V at Madrid would have to face challenges on two fronts, from Portugal, where the English enjoyed an alliance and could invade from the east, and from the Mediterranean, where naval preeminence allowed the Allies to sustain operations from the coast. For most of the year, the campaign of 1706 brought news as apocalyptic as that coming from Flanders and Italy. Philip V mounted an unsuccessful attempt to retake Barcelona, and in devoting so much to Barcelona, the Franco–Spanish forces were weak elsewhere. When an Allied army advanced from Portugal to take Madrid, Philip had to flee his capital in June. Just days after the Allies, led by Galway and Das Minas, marched into Madrid, they also took Saragossa, on 29 June. But the Allies proved unable to keep their hold on Madrid in the face of growing Franco–Spanish opposition and at the end of a long supply line, so they withdrew to Valencia, and Philip reentered his capital on 4 October.
In 1707 the Franco–Spanish forces rebounded to score important victories. Marshal Berwick, an illegitimate son of James II, advanced against Galway and beat him at the battle of Almanza on 25 April, a defeat that cost the Allies Valencia. Almanza did not win the war in Spain, but it surely set things on a different course. Valencia fell to the French on 8 May and Saragossa on 26 May. The Catalan town of Lerida capitulated to the Franco–Spanish forces on 14 October. Meanwhile, near the Portuguese border, Ciudad Rodrigo also passed into Philip’s hands on 4 October after a siege of nearly three weeks. By the close of 1707 Philip held most of Spain.
The French made modest gains in Catalonia during 1708, an otherwise rather uneventful year in Spain; Franco–Spanish armies also did little more than maintain the status quo in 1709. The next year threatened disaster at the outset and ended in triumph for Philip V. As an effort to encourage peace negotiations, Louis had withdrawn his generals and forces from Spain, and Philip V could not handle the Allied army led by its commander-in-chief, Starhemberg. That general defeated the Spanish at the Battle of Almenara on 27 July, won again at the Battle of Saragossa on 20 August, and finally took Madrid on 21 September.
However, with his hopes for peace dashed, Louis recommitted to Spain, sending Vendôme and reinforcements. With this pressure, Starhemberg retreated from Madrid and made for the coast with Vendôme on his heels. Vendôme caught the rear guard of the Allied army and defeated it on 9 December 1710 at the battle of Brihuega, and the next day Vendôme took on Starhemberg and the main body. This costly battle of Villa Viciosa ended in a tactical draw; Vendôme pushed Starhemberg from his position, but withdrew from the battlefield himself at the end of the day. Yet the consequences of the fight proved horrendous for Starhemberg, who abandoned his artillery and baggage in a rush for Catalonia. The French diplomat Torcy said of these battles, ‘No matter what, never has victory been more complete, and this day will change the face of affairs in Spain and at the same time those of Europe.’
The nature of the war changed in 1711. Louis and Philip had already secured irreversible victory in Spain by the close of 1710. Then, on 17 April 1711, Emperor Joseph I died, leaving Archduke Charles heir to the imperial crown and Habsburg lands. Should the Allies succeed in putting him on the throne of Spain, they would be creating a monarchy more dangerous to the balance of power in Europe than was a Spain ruled by Philip V. This undermined the determination of the alliance, and on 8 October the French and British concluded the ‘London Preliminaries,’ by which Louis pledged himself to recognize Anne and the Protestant succession in Britain and to ensure that the thrones of France and Spain were never united.
The last fighting saw Philip V consolidate his control over Spanish territory. He finally retook Barcelona on 12 September 1714 and regained Minorca in June 1715. The wars of the Sun King had ended, and within a few months Louis himself would pass from this earth.