Morocco–Spain relations
Morocco and Spain maintain extensive diplomatic, commercial, and military ties. Morocco’s foreign policy has focused on Western partners, including neighboring Spain. They have, however, been historically intense and conflictive.

History

Precedents 
After crossing the Strait of Gibraltar in 711, Muslims from North Africa led by the Umayyad commander Tariq ibn Ziyad seized the Visigothic Kingdom of Hispania in the wake of the Islamic conquest of the Iberian Peninsula. In the first generations after the conquest, the African Romance (argued to be part of a continuum with Ibero-Romance) presumably spoken by the Berber invaders may have facilitated communication with the native population, prior to the latter's arabization. Following the Abbasid takeover of the Umayyad caliphate, a branch of Umayyads established an independent Córdoba-centered Islamic polity in the Iberian Peninsula (initially an emirate and later a caliphate), which lasted until its demise in the early 11th century and ensuing replacement by ephemeral Islamic statelets. In the 10th century, Córdoba waged an expansionist policy to expand its clout in the Maghreb, vying a struggle against the Fatimid Empire.

Under the Almoravid and the Almohad dynasties, both of which had Marrakesh as a capital, most of the Muslim-controlled territory in the Iberian Peninsula came under Maghrebi imperial rule.

Vowing to counter the Castilian expansion initiated by 1265, Nasrid Granada required assistance from Fez in late 1274 and ceded the places of Algeciras and Tarifa to the Marinids, which thus gained a foothold in the southernmost end of the Iberian Peninsula. The Marinid grip over Algeciras further increased in the ensuing decades, and the place turned into a Marinid power base from which razzias were launched into the still incipient Christian settlements in the Lower Guadalquivir and the Guadalete area.

In the aftermath of the Granada War, the Catholic Monarchs, with the Alhambra Decree of 1492, ordered the expulsion of the Jews from the crowns of Castile and Aragon. Many of these Jewish refugees settled in the territory of current-day Morocco.  Some 40,000 Moriscos arrived there after their final expulsion in 1609, dressed in the Spanish way and speaking Spanish. Unlike their more welcoming situation in other places of the Maghreb such as Tunis, they were generally not well received, were accused of being Christians and sometimes suffered martyrdom. Morisco refugees from Hornachos founded the pirate Republic of Salé on the Bou Regreg river bank.

The cession of Larache effectively took place on 20 November 1610, when Juan de Mendoza y Velasco, Marquis of San Germán, assumed control over the Atlantic port of Larache on behalf of the Hispanic Monarchy. The harbour had been promised by Mohammed esh Sheikh el Mamun in exchange for the Spanish support in the internal struggles of the Saadi sultanate against his brother Zidan Abu Maali. The place remained under Spanish control until 1689, when it was seized by the troops of the Alaouite sultan Ismail Ibn Sharif.

In 1612, Spanish privateers stole the Zaydani Library, a collection of an estimated 4,000 manuscripts in literature and science belonging to Sultan Zidan bin Ahmad of the Saadi dynasty. These manuscripts are still kept at El Escorial.

After a period of good bilateral relations between the Moroccan and the Spanish crowns, hostilities resumed when the emboldened Alaouite sultan attempted to take Melilla in 1774 allied with the dey of Algiers. The failure of the siege and that of the ensuing Spanish retaliation persuaded both parties to enter peace negotiations, signing a peace treaty in 1780.

War and Spanish colonialism 
The Hispano-Moroccan War took place in 1859 and 1860. The casus belli for Spain were the unrelenting attacks of Riffian tribesmen on Spanish settlements in North Africa; following unfruitful negotiations with Sultan Abd al-Rahman vis-à-vis the reparations (the latter, unable to control the tribesmen, actually died in the midst of negotiations and was replaced by his brother Muhammad IV), a declaration of war propelled by Leopoldo O'Donnell was unanimously passed by the Spanish Congress of Deputies on 22 October 1859. In the Battle of Tetuan, the Mellah, or Jewish quarter, of Tetuan was sacked. This was followed by appeals in the European Jewish press to support Jewish communities like the one in Tetuan, leading to an international effort called "The Morocco Relief Fund."

Following an armistice of 32 days, the Treaty of Wad Ras or Peace of Tétouan was signed on 26 April 1860, bringing the conflict to an end. The treaty contemplated the extension on perpetuity of the Spanish presence in Ceuta and Melilla, the end of tribal raids on those cities, the recognition by Morocco of Spanish sovereignty over the Chafarinas Islands, the retrocession of the historical fortress of Santa Cruz de la Mar Pequeña (a territory of uncertain location by that time) to Spain to establish a fishing post, the permission to missionaries for establishing a Christian church in Tétouan, and the Spanish administration over the latter city until massive reparations of  duros were paid.

Once Morocco paid the compensation (partially through money lent by the British), O'Donnell retired his troops from Tétouan.

After 1863, a Spanish diplomatic mission led by Francisco Merry y Colom was sent to the court of the Moroccan Sultan in Marrakesh, with the specific goals of the rehabilitation of Muley El-Abbás, the sultan's hispanophile brother, the fostering of commercial activity in Ceuta and Melilla by means of the creation of a custom, the opening of the Port of Agadir to Spanish ships, facilitating the meat provision to Ceuta, and the improvement on the status of Spaniards in Morocco, establishing the basis for the peacetime commercial and diplomatic relations of Spain with the Sherifian Empire.

In the wake of the visit of a Spanish delegation to Fez in 1877, a joint Hispano-Moroccan committee was created to determine the location of the territory of Santa Cruz de la Mar Pequeña, retroceded in the 1860 Treaty of Wad Ras. This committee eventually misidentified Santa Cruz de la Mar Pequeña with Ifni, actually located about 480 kilometers north of the real fortress. The Moroccan sultan accepted the identification in 1883, even if the border delimitation did not take place at the time and the effective Spanish occupation had to wait until 1934.

The Spanish Protectorate over Morocco was established 27 November 1912 by decree of the Treaty Between France and Spain Regarding Morocco. Relative to France, which was assigned control over most of the Moroccan State, Spain ended up with a small territory in northern Morocco, largely mountainous and not easily accessible, and to which the Cape Juby strip, a small strip of land in Southern Morocco, bordering with the Spanish Sahara added up. The city of Tangier became an international zone.

21st-century relations 
The relations deteriorated following the ascension to the Moroccan throne of Mohammed VI in 1999. Particularly the failure to reach a deal for fisheries between the European Union and Morocco in 2001 further complicated the relations between José María Aznar and Mohammed VI.

In October 2001, Morocco recalled its ambassador from Madrid after pro-Saharan groups in Spain conducted a mock referendum on the fate of the region.

On 6 July 2002 Spanish military operations in the Alhucemas Islands were perceived to be an act of aggression by Morocco.

On 11 July 2002, the Perejil Island crisis erupted; members of the Royal Moroccan Navy occupied the uninhabited Perejil Island off the North-African coast; 6 days later Spain launched the "Operation Romeo-Sierra" and 28 members of the Special Operations Groups of the Spanish Army took control of the islet evicting the 6 Moroccan cadets then present in the islet, who offered no resistance.

Diplomatic ties were not restored until January 2003. That July, Morocco complained that Spain lacked neutrality on the Sahara issue when it chaired the United Nations Security Council and, in October, Spain suspended arms sales to Morocco due to the Perejil crisis. Spanish Prime Minister Jose Luis Rodriguez Zapatero visited Morocco in April 2004, and King Juan Carlos I visited in January 2005; on both occasions, joint statements called for a negotiated settlement to the Sahara issue—the Moroccan position.

However, visits to Ceuta and Melilla by the Spanish prime minister in January 2006 and monarchs in November 2007 again set back relations. The two neighbours also have an unresolved dispute concerning territorial waters between Morocco and the Spanish Canary Islands in the Atlantic Ocean. Morocco's "super port" near Tangier will pose competition that concerns Spanish ports. It is expected to achieve full capacity in 2014 at 5.5 million containers annually. 

Territorial disputes, despite their drama, are subordinate to the continuing and productive economic cooperation between both countries, there is also shared interests in counterterrorism, counternarcotics, and efforts to stem illegal immigration. Morocco notably assisted Spanish authorities in the investigation of the 2004 bombings in Madrid and this relationship continues. Moroccan soldiers have served under Spanish command in the United Nations Stabilization Mission in Haiti and Moroccan gendarmes have joined Spanish patrols to combat illegal immigration in the Strait of Gibraltar.

On 31 July-1 August 2018 Morocco indefinitely closed the Beni Ansar Customs near Melilla.

Following the breakdown of the ceasefire between the Polisario Front and Morocco, which led to armed clashes between both sides in November 2020, Spanish Second Deputy Prime Minister Pablo Iglesias tweeted the UN resolution of 1995 for the Western Sahara: "... It reiterates its commitment to hold, without further delay, a free, fair and impartial referendum for the self-determination of the people of Western Sahara ...". Unidas Podemos, minor member of the coalition government, requested the Prime Minister the same referendum to be held, clashing with the official position of the government expressed by Spanish Minister of Foreign Affairs Arancha González Laya.

On 21 December 2020, following the affirmations of the Moroccan Prime Minister, Saadeddine Othmani, stating that Ceuta and Melilla "are as Moroccan as the [Western] Sahara" after Trump's administration recognition of sovereignty over that territory, Spain's Secretary of State for Foreign Affairs Cristina Gallach urgently summoned the Moroccan Ambassador to Spain, Karima Benyaich, to convey that Spain expects respect from all its partners to the sovereignty and territorial integrity of its country and asked for explanations about the words of Othmani.

On 22 April 2021, Spanish officials had announced that the Polisario Front leader Brahim Ghali was sent to Spain for COVID-19 hospitalization. Morocco reacted with several reciprocal and retaliatory measures, including granting the pro-Catalan independence politician Carles Puigdemont asylum on 30 April.

On 17 May 2021, also as a response to Ghali's hospitalization in Spain, Morocco relaxed its controls over the frontier between the two countries and allowed more than five thousand Moroccan citizens and migrants (including more than 1,500 minors) to get through the Spanish city of Ceuta by passing around the jetties of Benzú and El Tarajal. This prompted the reaction of Spanish Prime Minister Pedro Sánchez, who visited Ceuta and Melilla together with the Interior Minister on 18 May.

In a cryptic manner, the Moroccan ambassador warned that "there are acts that have consequences and must be assumed" just before being recalled by the Ministry of Foreign Affairs on 18 June, in turn shortly after she was summoned by the Spanish foreign minister. The Spanish military was deployed in the border to stop the influx. Moroccan actions were also rejected by various officials of the European Union.

European Commission Vice President Margaritis Schinas warned that Europe would not "be intimidated by anyone on the subject of migration". The Moroccan move was described as an instance of "coercive engineered migration" and a case of grey zone operation, similarly to other asymmetrical challenges posed by Morocco underpinned by incremental and ambiguous measures below the threshold of war.

In a visit to Rabat, Prime Minister Pedro Sánchez announced on 18 March 2022 that Spain would shift its policy on Morocco's plan for Western Saharan autonomy and said that Morocco's plan was the only way to solve the problem. This, however, was refused by the vast majority of the Spanish congress, including PSOE's partners in government. On 20 March 2022, Morocco officially returned its ambassador Karima Benyaich to Madrid. 

At a February 2023 Summit in Rabat, Spain and Morocco subsequently signed a series of multiple agreements to boost trade and investment, including credit lines of up to 800 million euros ($873 million). A joint declaration reiterated the Spanish Government's position supporting Morocco's autonomy plan for Western Sahara. Both countries were reported to have further agreed to cooperate on repatriations of illegal migrants.

As of March 2023, Spain's Minister of Foreign Affairs, European Union and Cooperation, Jose Manuel Albares, shared that the Morocco-Spain relationship has achieved its new all-time high.

On 22 June 2023, according to Human Rights Watch, the victims of the horrific violence and deaths of asylum seekers and migrants at the border in 2022 between Morocco and the Spanish enclave of Melilla didn't get justice.

Resident diplomatic missions

of Morocco in Spain
 Madrid (Embassy)
 Algeciras (Consulate-General)
 Almería (Consulate-General)
 Barcelona (Consulate-General)
 Bilbao (Consulate-General)
 Girona (Consulate-General)
 Las Palmas de Gran Canaria (Consulate-General)
 Murcia (Consulate-General)
 Palma de Mallorca (Consulate-General)
 Seville (Consulate-General)
 Tarragona (Consulate-General)
 Valencia (Consulate-General)

of Spain in Morocco
 Rabat (Embassy)
 Agadir (Consulate-General)
 Casablanca (Consulate-General)
 Nador (Consulate-General)
 Tanger (Consulate-General)
 Tetuan (Consulate-General)
 Larache (Consulate)

Common history 
Hispania
Caliphate of Cordoba
Taifa of Ceuta (in Ceuta and Tangier)
Almoravid Empire
Almohad Empire
Benimerin Empire
Spanish protectorate in Morocco (1912-1956)
Spanish Sahara (1884-1976)
Ifni (1860-1969)
Tangier International Zone (1940-1945)

See also
Moroccans in Spain
Morocco-Spain border
Spanish Protectorate of Morocco
Spanish Sahara
List of Spanish colonial wars in Morocco

References
Informational notes

Citations

 
Spain
Bilateral relations of Spain
Relations of colonizer and former colony