The very last time Pepe put on his Real Madrid kit and ran on to the pitch, it wasn’t to play. Képler Laveran Lima Ferreira joined team-mates in celebrations at the Santiago Bernabéu on Sunday evening wearing all white, knowing that he will not be back. The day before, he had been left out of the squad for the Champions League final at the Millennium Stadium; the night after, he finally confirmed what had long been an open secret: “It’s clear that I won’t be continuing at Madrid,” he said. There was sadness as he said it. There was a touch of bitterness, too. Ten years on, he was going.

At 34 and into the final year of his contract, Pepe had publicly said he would wait for Madrid “until the end”, but eventually gave up in April. He wanted to stay and asked for a two-year deal; Madrid offered only one. Most of his stuff has been in storage for the past two months, shipped back to Portugal, awaiting his next destination, which appears likely to be France. He knew it would end but it was not supposed to be like this. Everyone else knew it too, even as he kept silent – not least because he kept silent, in fact. He was the only player, with Fábio Coentrão, nowhere to be seen at the club’s pre-final media day.

When Madrid won the league in Málaga, Pepe filmed celebrations from the touchline, selfie stick in hand. In Cardiff, he joined them having watched the game from the stands. When they returned, visiting the cathedral and the town hall, he hung back. At the Bernabéu celebrations there was a photo with Cristiano Ronaldo in the dressing room – “with my friend Pepito”, Ronaldo wrote – before his farewell in front of the fans. He didn’t say goodbye to Zinedine Zidane, he admitted pointedly, as the coach already knew he was off – before he himself had.

The following night, party packed up, Pepe said that Madrid’s treatment of him had “not been right”. His complaints, made on the radio station Cadena Cope, have changed the atmosphere, ending things in a way that few expected, but beyond that immediate irritation, there will surely be gratitude for the past 10 years. Pepe said that Madrid had not protected him amid accusations of tax evasion; admitted that, unusually among the squad, he had connected better with Rafa Benítez than with Zidane; and expressed his disappointment that no one from the board had called him after he had cracked his ribs against Atlético Madrid on 8 April.

As it turned out, that was the last time he ever played for Madrid. Before he had been forced to withdraw, he had scored the goal that had put Madrid into the lead. He celebrated with a raised finger, Madrid badge held between his teeth. That image felt appropriate; fans at the Bernabéu have long appreciated him, celebrated him. Team-mates, too. When Pepe arrived from Porto for €30m, one question was asked repeatedly: “Who is Pepe?!” He leaves with three league titles, three European Cups and having played 334 games. Only five foreigners ever played more than the Brazilian-born Portugal defender.

Play Video 1:32 Real Madrid celebrate at Bernabéu after Champions League win – video

Who is Pepe? For a long time, the answer from some was a villain, public enemy No1. Rival supporters chanted: “Assasin! Assasin!” at him. Catalan TV3 depicted him as Hannibal Lecter; Madrid tried, and failed, to sue. Few forgot that image of him treading on Leo Messi’s hand. In the aftermath of a series of tense clásicos in 2012 which turned genuinely nasty, he accused Barcelona’s players of acting. Andrés Iniesta admitted that he was offended, but scoffed: “What’s Pepe going to say?” He added: “We could do a whole video of his fouls. We all know him.”

On the pitch, perhaps. Off it, he was one of the most likable members of Real Madrid’s squad: approachable, friendly, funny, even gentle. It is not so unusual: there are plenty of players like that, men whose team-mates love them even as they admit they are transformed out there. Pepe was always remembered best for his attack on Getafe’s Javier Casquero in April 2009. At the time, there was no doubt that he was one of the toughest, most aggressive defenders in Spain, often a little wild, sometimes dirty, but that night was something else.

Pepe pushed Casquero over, kicked him twice in the back, raking his studs across Casquero’s spine, punched Juan Albín, and walked off calling them all “sons of bitches”. He was handed a 10-game ban, the sixth-biggest in Spanish football history, allowing some in the capital to present it as if he were the victim. Without him, Madrid lost five of the next six, the title slipping away.

He was hurt, he said. He said he didn’t recognise himself; he had gone “absolutely mad”. He described those days as “the worst days of his life”, and said he had spent much of the time in tears. Madrid’s full-back Marcelo said: “I have never seen him like this.” Pepe apologised to the Getafe players and to “football as a whole” but not to his team-mates, at least not straight away. “I suppose he doesn’t want to talk about it,” Christoph Metzelder said. “I hope he will soon be the happy lad we all know.” Pepe said: “I’m not sure I have the will to keep playing at the moment.”

He did, of course. He sought professional help to control himself. He didn’t exactly become an angel and certainly not straight away – those battles with Barcelona were still three years off, and fans celebrated his aggressiveness – but nothing quite like that happened again. And with time, maturity, his game did change, as did people’s perceptions – especially after the departure of José Mourinho. Pepe’s last red card for Real Madrid was in 2011. He has not been in double figures for yellow cards for four seasons.

Although some clung to that image, it wasn’t him. He came to be seen not just as a pantomime bad guy but a player and a very good one. The chant “Pepe, mátalo!” became “Pepe, marca un gol!”: they had gone from “Pepe, kill him!” to “Pepe, score a goal!”. In truth, his own fans had always liked the bad guy, too, but mostly they liked this defender: reliable, dependable, committed. He had become a symbol for the club, a captain without the armband. It was reported on Tuesday morning in Spain that the club were arranging a farewell for him. Sources at the club were quoted as calling his commitment and performance impeccable. At that point, they had not yet heard his comments from the night before.

Increasingly, he led. There was personality on and off the pitch that was appreciated. He ended up falling out with Mourinho when he publicly stood up for Iker Casillas, calling out the coach for his treatment of the club captain. Mourinho replied that, at heart, Pepe’s problem was that he was a 30-year-old who couldn’t take being “run over” by the 19-year-old Raphaël Varane. The manager described it as “life”, but there was life left in Pepe.

David Squires on … Real Madrid winning the Champions League again Read more

At that point, in May 2013, he was thinking about a departure but Carlo Ancelotti convinced him that he would be important. He trusted in Pepe. Varane was talented, he said, but “Pepe is Pepe”. Ancelotti said: “We’re more assured with him. He gives us confidence, personality and he helps the other players; he is very important for them.” The Italian coach always spoke admiringly of the defender’s honesty and professionalism in admitting that he wasn’t fully fit before the 2014 European Cup final against Atlético in Lisbon. In Milan, two years later, he started, aged 33, and he was superb. There is an argument to suggest that he was also Portugal’s best player at Euro 2016. A year on, injuries interrupted his season, although he impressed every time he was available, and the contract he waited for never came. On Monday night he admitted what had become an open secret.

Pepe once said that when he retires he would like to travel the world in a caravan. That’s still some way off. He believes he has matches left in him, just not, he now knows, for Madrid.