Abdelbaki Es Satty, who died in an explosion last Wednesday, is believed to have masterminded the attacks

The Imam accused of masterminding the Barcelona terrorist attacks was ordered to leave Spain after finishing a prison sentence for drugs offences, it was claimed today.

Islamic preacher Abdelbaki Es Satty was told he must comply with an expulsion order when he left jail in April 2014, respected Spanish daily El Mundo said.

The paper said a first appeal against the decision failed - but the 42-year-old Moroccan won his fight to stay in Spain after taking the case to a judge.

The unnamed judge who made the decision is said to have accepted Es Satty's claims his forced removal would be a breach of his international rights.

The preacher followed up his court win by seeking asylum through lawyers in an application filed on November 29 2014, El Mundo reported.

The paper described the asylum request as a 'standard move to legitimise even further his presence in Spain,' and said it enabled him to move freely among the 26 EU countries that form part of the Schengen area.

He died at a home in the town of Alcanar, where butane gas canisters were being stockpiled for use in the Barcelona attack

Es Satty, who police confirmed last night had died in an explosion at a house in the Catalan town of Alcanar said to have prompted last Thursday's deadly attacks in Barcelona and the seaside resort of Cambrils, has been blamed for brainwashing the terrorists.

More than 20 butane gas canisters were found intact in the wreckage of the home, as well as Acetone peroxide (TATP) - nicknamed 'Mother of Satan' because of the high number of accidental explosions it causes.

Hans Bonte, the mayor of Vilvoorde near Brussels, has already revealed the Imam spent three months there at the start of last year looking for work.

There was no immediate official response to the reports about Es Satty's removal order, which were also carried by news website Okdiario.

It said it was linked to the four-year prison sentence he was handed in 2010 after being caught trying to smuggle cannabis resin on a ferry between Spain's North African enclave of Ceuta and the southern Spanish port city of Algeciras.

Fifteen people were killed and more than 100 injured in last week's terror attacks in Barcelona and Cambrils by the terror cell

Under Spanish immigration laws, foreign-born nationals who receive prison sentences of more than one year often face the threat of expulsion when they leave jail.

Es Satty is said to have learned he had won his fight against his expulsion order on March 2 2015 in a sentence drafted by Castellon's Contentious-Administrative Court Number 2.

Younes Abouyaaqoub, the driver of the van that mowed down and killed 14 people on Barcelona's Las Ramblas before stabbing Spaniard Pau Perez to death and stealing his car

He had been out of prison for nearly a year when he was told the news.

Enraged Javier Pina, commenting on the reports on social media, said: 'Never mind what his conscience is telling him, if he has one. The judge who took this decision should be called to account over this.'

The court that took the decision on Es Satty is said to be based in the eastern Spanish city of Castellon, where he served his sentence.

His removal from Spain could have changed the course of history, as it would almost certainly have prevented him from entering into contact with the men accused of bringing terror to Spain last week in attacks which killed 15 people and left more than 100 injured.

Today it emerged police are focusing on the theory that Younes Abouyaaqoub, the driver of the van that mowed down and killed 14 people on Barcelona's Las Ramblas before stabbing Spaniard Pau Perez to death and stealing his car, spent his time on the run sleeping by day and walking at night to minimise his chances of being detected by CCTV cameras or noticed by members of the public.

Catalan daily La Vanguardia said he had changed his T-shirt before he was gunned down by police in a vineyard in Subirats an hour's drive west of Barcelona - but was dirty and had no phone, money or belongings on him other than a fake explosives belt and three knives.