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. (See Muhammad Ibn 'Abd al-Karim al-Khattabi, Annual). 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 unpopularity 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.