Mariano Rajoy was the patient man of Spanish politics, rewarded in 2011 at his third attempt to lead the conservative Popular Party (PP) to victory in a general election.
Then, seven years later, he became the first prime minister in modern Spanish history to be forced from office by a no-confidence vote.
Mr Rajoy's supporters had seen him as the crisis manager who averted an economic bailout, a quietly effective leader capable of maintaining the country's unity as the separatism movement hit tense heights in the north-eastern region of Catalonia.
Yet his critics accused him of favouring economic shock therapy, hitting the poorest the hardest, of being heavy-handed towards the separatists, and turning a blind eye to corruption in his own party.
Friend and foe alike agreed that the lanky man in glasses was not the most charismatic of politicians but Mr Rajoy, in fairness, did not promise fireworks.
The rise through the ranks
Born the Roman Catholic son of a lawyer in 1955, Mr Rajoy was brought up in Santiago de Compostela, north-western Spain, and after studying law became a land registrar.
Elected a PP regional deputy at the age of 26, he climbed through the ranks, serving as a minister in the Aznar governments of 1996-2004.
He held various ministerial roles before taking the leadership for the 2004 election.
Despite being tipped to win a third term, the PP crashed out of office; its shock defeat blamed on the Madrid train bombings and the outgoing prime minister's handling of the atrocity.
As leader of the opposition, Mr Rajoy struggled to rebuild his party's fortunes and was defeated again in 2008, though the party increased its share of the vote and seats.
Third time lucky
Mr Rajoy won the 2011 general election on a promise to tackle the aftermath of the 2008 economic crisis.
He oversaw an EU-backed period of austerity, steering Spain back to growth, though critics say much of the recovery is fragile and often the only jobs people can get are low-paid and insecure.
The PP party lost its majority at the inconclusive 2015 general election but soldiered on, increasing its share of the seats in the following year's poll to form a minority government.
One of the biggest challenges of Mr Rajoy's time in office was the Catalonia crisis.
When Catalan separatists held a referendum in October 2017 and declared independence unilaterally, Mr Rajoy's government declared the move illegal and dissolved the region's parliament.
Defeated by parliament
Mr Rajoy was forced out of office by a no-confidence vote in parliament on 1 June 2018.
The opposition said he had failed to take responsibility for his party's involvement in a corruption scandal. The criticism had been bubbling for some time but gathered strength after a former PP treasurer was convicted of receiving bribes, money laundering and tax crimes.
As Mr Rajoy departed, he told Spanish MPs that it was "an honour to leave a better Spain than I found".
He will be succeeded by Pedro Sánchez from the Spanish Socialist Party, the party that triggered the no-confidence vote.