The military history of Spain, from the period of the Carthaginian conquests over the Phoenicians to the former Afghan War spans a period of more than 2200 years, and includes the history of battles fought in the territory of modern Spain, as well as her former and current overseas possessions and territories, and the military history of the people of Spain, regardless of geography.
Spain's early military history emerged from her location on the western fringes of the Mediterranean, a base for attacks between Rome and Carthage. With the fall of the Roman Empire, Spain was devastated by successive barbarian invasions, with stability only gradually appearing with the later years of the Visigothic kingdom. The early Middle Ages for Spain saw the country forming the front line in a battle between Christian and Islamic forces in the Mediterranean; the Conquista and Reconquista took centuries to reach a military resolution. The period from 1492, when the Reconquista was completed and the Spanish colonization of the Americas began, to the late 17th century is known as the Spanish Golden Age. Spain acquired a vast empire by defeating the centralised states of the Americas, and colonising the Philippines. Her tercio units, backed by imperial gold and silver, were involved in most the major wars of the period. It was not until the years after the Thirty Years' War that Spanish military power began to fade; even then, supported by a reinvigorated navy, Spain remained a major military power throughout the 18th century, in competition with Britain and France on the global stage.
The Napoleonic Wars changed Spanish military history dramatically; the Peninsular War saw the development of guerrilla warfare against the occupying French forces. The collapse of central Spanish authority resulted in successful wars of independence amongst Spain's American colonies, drastically reducing the size of her empire, and in turn led to a sequence of civil wars in Spain itself, many fought by frustrated veterans of the French and colonial campaigns. Attempts to reassert imperial power during the mid-19th century, enabled by the development of the steam frigate ultimately failed, leading to the collapse of the remnants of Spain's empire in the Americas and Asia in 1898 at the hands of a rising power, the United States of America. The political tensions that had driven the Carlist Wars remained unchecked, spilling over once again in the Spanish Civil War of 1936–39. Bringing a foretaste of the tactics of the Second World War, several nations used the conflict as a testing ground for new aerial and armoured warfare tactics. In the post-war period, Spain has increasingly turned away from the last remaining colonial conflicts in Africa, and played a growing modern military role within the context of the NATO alliance.