1. Prison sentences of more than one year imposed on a foreign national shall be replaced by their expulsion from Spanish territory. Exceptionally, when necessary to ensure the defense of the legal order and restore confidence in the validity of the law violated by the crime, the judge or court may order the execution of a portion of the sentence, not exceeding two-thirds of its length, and the replacement of the remainder with the expulsion of the convicted person from Spanish territory. In any case, the remainder of the sentence shall be replaced by the expulsion of the convicted person from Spanish territory when they are granted parole or conditional release.
2. When a prison sentence of more than five years has been imposed, or several sentences exceeding that duration, the judge or court shall order the execution of all or part of the sentence, to the extent necessary to ensure the defense of the legal order and restore confidence in the validity of the law violated by the crime. In these cases, the execution of the remainder of the sentence will be replaced by the expulsion of the convicted person from Spanish territory when the convicted person has served the portion of the sentence that has been determined, is granted parole, or is released on parole.
3. The judge or court will rule in the judgment on the substitution of the execution of the sentence whenever possible. In other cases, once the judgment has become final, the judge or court will rule as quickly as possible, after hearing the Public Prosecutor and the other parties, on whether or not to grant the substitution of the execution of the sentence.
4. Substitution will not be granted when, in light of the circumstances of the offense and the personal circumstances of the offender, particularly their ties to Spain, expulsion would be disproportionate.
The expulsion of a citizen of the European Union will only be granted when they represent a serious threat to public order or public safety, taking into account the nature, circumstances, and seriousness of the crime committed, their criminal record, and personal circumstances.
If the person has resided in Spain for the previous ten years, expulsion will be warranted when, in addition:
a) They have been convicted of one or more crimes against life, liberty, physical integrity, and sexual freedom and integrity punishable by a maximum prison sentence of more than five years, and there is well-founded evidence of a serious risk that they may commit crimes of the same nature.
b) They have been convicted of one or more terrorism offenses or other crimes committed within a criminal group or organization.
In these cases, the provisions of paragraph 2 of this article will apply in all cases.
5. The foreign national may not return to Spain for a period of five to ten years from the date of expulsion, taking into account the length of the substituted sentence and the personal circumstances of the convicted person.
6. Expulsion will entail the dismissal of any administrative proceedings aimed at obtaining authorization to reside or work in Spain.
7. If the expelled foreigner returns to Spain before the judicially established period has elapsed, they shall serve the sentences that were substituted, unless, exceptionally, the judge or court reduces their duration when their fulfillment is deemed unnecessary to ensure the defense of the legal order and restore confidence in the legal norm violated by the crime, taking into account the time elapsed since the expulsion and the circumstances in which the breach occurred.
However, if they are apprehended at the border, they shall be expelled directly by the government authority, and the entire period of the entry ban shall begin to run again.
8. When, at the time of the expulsion order in any of the cases provided for in this article, the foreigner is not or is not effectively deprived of liberty in execution of the imposed sentence, the judge or court may order, in order to ensure the expulsion, their admission to an immigration detention center, under the terms and with the limits and guarantees provided by law for government expulsion.
In any case, if, after the substitution of the custodial sentence with expulsion has been agreed upon, the expulsion cannot be carried out, the originally imposed sentence or the remaining period of the sentence shall be executed, or, where applicable, the suspension of its execution shall be applied.
9. Sentences imposed for the commission of the offenses referred to in Articles 177 bis, 312, 313, and 318 bis shall not be substituted.