Although the former queen, Isabella II was still alive, she recognized that she was too divisive as a leader, and abdicated in 1870 in favor of her son, Alfonso, who was duly crowned Alfonso XII of Spain. After the tumult of the First Spanish Republic, Spaniards were willing to accept a return to stability under Bourbon rule. The Republican armies in Spain which were resisting a Carlist insurrection pronounced their allegiance to Alfonso in the winter of 1874-1875, led by Brigadier General Martinez Campos. The Republic was dissolved and Antonio Canovas del Castillo, a trusted advisor to the king, was named Prime Minister on New Year's Eve, 1874. The Carlist insurrection was put down vigorously by the new king, who took an active role in the war and rapidly gained the support of most of his countrymen.
A system of turnos was established in Spain in which the liberals, led by Práxedes Mateo Sagasta and the conservatives, led by Antonio Canovas del Castillo, alternated in control of the government. A modicum of stability and economic progress was restored to Spain during Alfonso XII's rule. His death in 1885, followed by the assassination of Canovas del Castillo in 1897, destabilized the government.
Cuba rebelled against Spain in the Ten Years' War beginning in 1868, resulting in the abolition of slavery in Spain's colonies in the New World. American interests in the island, coupled with concerns for the people of Cuba, aggravated relations between the two countries. The explosion of the USS Maine launched the Spanish-American War in 1898, in which Spain fared disastrously. Cuba gained its independence and Spain lost its remaining New World colony, Puerto Rico, which together with Guam and the Philippines were ceded to the United States for 20 million dollars. In 1899, Spain sold its remaining Pacific islands the Northern Mariana Islands, Caroline Islands and Palau to Germany and Spanish colonial possessions were reduced to Spanish Morocco, Spanish Sahara and Spanish Guinea, all in Africa.
The "disaster" of 1898 created the Generation of '98, a group of statesmen and intellectuals who demanded change from the new government. Anarchist and fascist movements were on the rise in Spain in the early twentieth century. A revolt in 1909 in Catalonia was bloodily suppressed.
Spain's neutrality in World War I allowed it to become a supplier of material for both sides to its great advantage, prompting an economic boom in Spain. The outbreak of Spanish influenza in Spain and elsewhere, along with a major economic slowdown in the postwar period, hit Spain particularly hard, and the country went into debt. A major worker's strike was suppressed in 1919.
Mistreatment of the indigenous population in Spanish Morocco led to an uprising and the loss of this North African possession except for the enclaves of Ceuta and Melilla in 1921. In order to avoid accountability, King Alfonso XIII decided to support the dictatorship of General Miguel Primo de Rivera, ending the period of constitutional monarchy in Spain.
In joint action with France, the Moroccan territory was recovered (1925-1927), but in 1930 bankruptcy and massive un popularity left the king no option but to force Primo de Rivera to resign. Disgusted with the king's involvement in his dictatorship, the urban population voted for republican parties in the municipal elections of April 1931. The king fled the country without abdicating and a republic was established.