The European Union's top court dealt a blow to Barcelona, Real Madrid and two other Spanish soccer clubs on Thursday by upholding a decision from the bloc's executive arm ordering they should pay back illegal state aid.

In its final ruling, the European Court of Justice canceled a previous legal decision two years ago by a lower EU court that found the clubs’ tax regime was lawful, and said the action brought by Barcelona is “definitively rejected."

In 2019, the Luxembourg-based General Court annulled a decision by the European Commission dating back to 2016 ordering the clubs to repay several million euros in tax compensations. The EU's executive arm had found at the time that public support measures granted by Spain to several professional soccer clubs gave them an unfair advantage over other teams, in breach of EU state aid rules.

Real Madrid's Sergio Ramos, centre, reacts during a Spanish La Liga soccer match between Osasuna and Real Madrid at El Sadar stadium in Pamplona, Spain, Saturday, Jan. 9, 2021. (AP PhotoAlvaro Barrientos)

Real Madrid's Sergio Ramos, centre, reacts during a Spanish La Liga soccer match between Osasuna and Real Madrid at El Sadar stadium in Pamplona, Spain, Saturday, Jan. 9, 2021. (AP PhotoAlvaro Barrientos)

When the General Court annulled the decision, it said the commission had not proved the tax regime constituted an unlawful economic advantage. But the ECJ ruled that the lower court committed an error in law and observed that the measures which also benefited Osasuna and Athletic Bilbao indeed constituted an aid scheme covering an unspecified amount of money and time, and was not linked to a specific project.

The Commission said the four clubs were treated as non-profit organizations and paid a 5% lower tax rate on profit than rivals during more than 20 years, without an objective justification. It said the money to be recovered would be limited to 5 million euros ($6 million) per club but that the precise amount should be fixed by Spanish authorities.

Barcelona has been going through a turbulent week after former president Josep Bartomeu appeared before a judge following a night in jail while being investigated for possible irregularities during his administration. Bartomeu and other officials were arrested on Monday after Catalan police raided Barcelona’s headquarters in a search and seizure operation.

The arrests came less than a week before the club holds presidential elections.

Barcelona is coming off its first season without a trophy since 2007-08 and has a debt of more than 1.1 billion euros ($1.3 billion), largely because of the coronavirus crisis.

More AP soccer: https://apnews.com/hub/soccer and https://twitter.com/AP_Sports