The Russian Federation and the Kingdom of Spain, a member state of the European Union, have bilateral foreign relations. Spain and the Grand Duchy of Moscow first exchanged envoys in 1520s; regular embassies were established in 1722. The two countries share a long history of relations, characterized at times by close cooperation and at other times by deep hostility. Soviet-Spanish relations, once terminated after the Spanish Civil War, were gradually reestablished starting in 1963 and were fully established by 1977. Trade between the two countries amounted to two billion euros in 2008. In March 2009, the two countries signed an energy agreement providing national energy companies access to other party's domestic markets. Spain, a member of the European Union and NATO, sanctioned Russia after its invasion of Ukraine.

Muscovy and Imperial Russia Official contacts between the Spanish Empire and the Grand Duchy of Moscow go back to 1519, when King Charles I of Spain notified Grand Duke Vasili III of Russia of his ascension to the throne of the Holy Roman Empire. In 1523, Yakov Polushkin delivered Vasili's response to the court at Valladolid, thus becoming the first Russian envoy to Spain. In 1525 Russian envoys Ivan Zasekin-Yaroslavsky and Semyon Borisov presented their credentials to Charles; they brought news of the discovery of the Americas to Muscovy.

The Spanish Inquisition and especially the aftermath of the Alhambra Decree expelling Jews from the Kingdom were of particular interest to 16th-century Russian Orthodox clergy who had the interest to suppress the Judaizers heresy within the Russian Orthodox Church.Hillgarth, p. 481 Muscovy, unlike contemporary European nations, studied the Inquisition not "as an example to avoid but a model to imitate".

Another temporary contact was established by Pyotr Potemkin's embassy (1667–1668) during the reign of Alexis I of Russia.Hillgarth, p. 480

Regular embassies of the two countries were established by Peter I of Russia and Philip V of Spain in 1722; in 1723, the Russian Empire also opened a consulate in Cádiz. Prince Sergey Golitsyn served as the first Russian ambassador to Spain; duke Diego Francisco de Liria, who also inherited the Jacobite title of Duke of Berwick, served as ambassador of Spain in Russia. However, after the unexpected death of Peter II of Russia, Spain declared the ascension of Anna of Russia unlawful and severed diplomatic relations until 1759. De Liria, who closely watched the events of 1730, provided an important account of Anna's ascension.Anisimov, pp. 80-84

In 1799–1801 Spain severed ties after Paul I of Russia assumed the Catholic title of Grand Master of Knights Hospitaller; in 1833–1856 Russia closed the embassies, denying legitimacy of Isabella II of Spain. Apart from these two conflicts, relationships were uneventful; the two countries were never engaged in direct war against each other. In 1756–1763 they were allies in the Seven Years' War. During the Napoleonic Wars the two countries were both allies and foes, but never engaged each other directly.

While Russian intelligentsia commented favorably regarding colonial rebels during the decolonization of the Americas, Tsar Alexander I (1777-1825) supported the efforts to suppress rebellion by Spain's King Ferdinand VII (1784-1833). Plans for sales of warships were kept secret to avoid British interference. In August 1817, Russia sold the Spanish Navy five ships of the line and three frigates. They were delivered to Cádiz in February 1818, however were found to be largely unseaworthy.Russell H. Bartley, Imperial Russia and the Struggle for Latin American Independence, 1808–1828 (1978), 122-124. Russia ignored the independent Latin American states until the late 19th century.William Spence Robertson, "Russia and the Emancipation of Spanish America, 1816-1826: Hispanic American Historical Review 21#2 (1941), pp. 196-221 DOI: 10.2307/2507393 online

In 1893, Director of Police Pyotr Nikolayevich Durnovo caused a minor diplomatic incident by ordering secret agents to steal correspondence between the Spanish Ambassador and his mistress, with whom Durnovo had become smitten. The crisis ended when the ambassador complained to Tsar Alexander III, who dismissed Durnovo.Figes, Orlando (1996). A People's Tragedy: The Russian Revolution, 1891-1924. London: Jonathan Cape. pp. 168–169. . .

During the Russian Revolution, Alfonso XIII attempted the exfiltration of the Romanov family from Russia to Spain after the abdication of Nicholas II. Grand Duchess Maria Vladimirovna was born in Madrid on December 23, 1953 and, currently, she lives in the Spanish capital with her son George Mikhailovich Romanov, who was born on March 13, 1981.

Soviet period The Soviet Union established diplomatic relations with the Second Spanish Republic on July 28, 1933. Moscow for years tried to purify the Spanish Communist Party by expelling anarchist and Trotskyist members, but the process took years and was finally handled by outside Communists sent to Spain in the Spanish Civil War who exposed and executed opponents.Tim Rees, "Deviation and discipline: anti-Trotskyism, Bolshevization and the Spanish Communist party, 1924–34." Historical Research 82.215 (2009): 131-156. Ambassador Marsel Rosenberg (1896–1938), his adviser and successor Leon Gaikis (1898–1937), and Consul-general Vladimir Antonov-Ovseenko (1883–1938) arrived in Madrid in 1936, when Spanish Civil War was already underway. The three were soon recalled to Moscow and executed for an alleged Trotskist conspiracy.

The Soviet Union actively supported the Republican faction through the course of the Civil War with military advisers, "volunteers" and weapons supplied in exchange for Bank of Spain gold reserves later known as Moscow gold (see Foreign involvement in the Spanish Civil War). The monument to Soviet volunteers in Madrid, inaugurated in 1989 by mayor Juan Barranco Gallardo and Soviet ambassador Sergey Romanovsky, lists 182 names of identified Soviet combatants killed during the war. With the fall of Republicans and the victory of the Nationalists in 1939, the Soviet Union lost all ties with Spain, now under Franco. During World War II the Blue Division of Spanish volunteers in the Wehrmacht fought against the Soviet Red Army on the Eastern Front until Francisco Franco removed them. Franco steered away from direct participation in the war and maintained Spain's neutrality.

Spain's relations with the Soviets after World War II were described as "the worst, though hardly the most problematic",Payne, p. 531 culminating in Nikita Khrushchev's speech against Franco's regime in the United Nations General Assembly on October 1, 1960 and Franco's ban on the Spain vs. USSR game of the 1960 European Nations' Cup scheduled earlier in the same year.Spanish withdrawal from this tournament paved way to Soviets winning the 1960 title. Soon, however, the same leaders began gradual reestablishment of contacts. In April 1963 Khrushchev and Franco exchanged letters on disarmament and the fate of Julián Grimau; in January 1964 Franco appealed to Khrushchev again. According to Soviet explanation of events, the move was initiated by the Spanish government.

Until 1969, relations were informally maintained through Soviet and Spanish embassies in France. In 1967 Spanish and Soviet representatives agreed to open their seaports to ships carrying flag of the other country; in 1969 the Soviet state-owned Black Sea Shipping Company opened an office in Madrid – the first Soviet establishment in Spain since the Civil War. This office, staffed by professional diplomats, and headed by Sergey Bogomolov, who relocated to Madrid from Paris, doubled as the de facto Soviet consulate.

The two countries signed an agreement on foreign trade in 1972. It promoted amicable diplomatic relations and the strategic partnership between the two countries.Yu. Korchagin and A. Rubinchik, "Towards a Strategic Partnership: On the 40th Anniversary of the Trade Agreement Between Russia and Spain" International Affairs: A Russian Journal of World Politics, Diplomacy & International Relations (2012) 58#6 pp 146-152. They established permanent trading missions in 1973; these offices assumed and maintained consular duties. Igor Ivanov, future foreign minister of independent Russia, has served in Madrid in 1973–1983.Gladman, p. 586 After Franco's death, the Soviet Union and Spain reestablished full diplomatic relations on February 9, 1977. Bogomolov assumed the title of Soviet ambassador and presented his credentials to king Juan Carlos I of Spain on May 5, 1977. Relations of this period were not exactly friendly, marred with mutual expulsion of alleged spies operating under diplomatic immunity; things got worse when Spain was admitted into NATO in 1981, an act regarded by the USSR as a "violation of Soviet interests".

After Bogomolov moved up to a senior position in the Ministry of Foreign Affairs in 1978, he was replaced by Yuri Dubinin, who steered Soviet policy in Spain through the last phase of the Cold War, until 1986. Dubinin actively promoted the idea of a state visit by Juan Carlos to Moscow; only after five years of preparations did Andrei Gromyko approve the visit that materialized in May 1984. Alexander Igorevich Kuznetsov, former (2005 to 2012) Russian ambassador to Spain and Andorra, has served in Madrid under Dubinin in 1982–1986.

The Embassy of Russia in Madrid, inherited from the Soviet Union, was built in 1986–1991. The land lot at 155, Calle de Velázquez, was provided to the Soviets in 1980, but was loaded with zoning regulations limiting building height to 6 meters, presence of Spanish Armed Forces cables running underground and a gypsy squat town sprawling above – these obstacles held off construction for years. The building was initially designed by painter Ilya Glazunov; Glazunov later contributed interior design, building structure was redesigned by architect Anatoly Polikarpov.

Russian Federation 1991-2021 Spain and post-Communist Russia established diplomatic relations on December 9, 1991. This resulted in improved economic, trade, and cultural relations. There were museum exhibition exchanges and several visits between the two countries' leaders including Prime Minister Vladimir Putin, Russian President Dmitry Medvedev, and Spanish King Juan Carlos I. They dealt with the issue of Kosovo's independence from Serbia, Spain's chairmanship of the European Union (EU) in 2010, and foreign energy policy.N. Anikeeva, and Yu. Dubinin, "Moscow-Madrid: Cooperation Milestones." International Affairs: A Russian Journal of World Politics, Diplomacy & International Relations (2011) 57#3 pp 214-221 Igor Ivanov, a veteran of the Soviet embassy in Madrid, was appointed Ambassador of Russia to Spain and served in Madrid until 1994.Ivanov, preface In April 1994 president Boris Yeltsin became the first Russian head of state to pay a state visit to Spain. Juan Carlos visited Russia in 2002, 2006, 2008 and 2012.President of Russia - Meeting with King Juan Carlos I of Spain

Spain's share in Russian foreign trade in early 2000s hovered at just above 1% of Russian exports (dominated by oil and raw materials); Spanish exports to Russia were significantly lower (2001: 488 million US dollars vs. 890 million).Gladman, p. 330 In 2008, according to Dmitry Medvedev, foreign trade levelled at 2 billion Euro (less than 1% of either country's foreign trade) while that between Russia and other countries comparable to Spain is measured in tens of billion euro. Spain's share in foreign direct investment to Russian economy remains insignificant, the largest investment (as at 2003), at 319 million US dollars, has been made by Segura Consulting.

In December 2000 the relations were strained by Spain' refusal to extradite fugitive banker and media executive Vladimir Gusinsky. Gusinsky, arrested and released in Spain, emigrated to Israel in April 2001.Gladman, p. 307