Article 129 bis [source]

Spanish Criminal Code
English translation

If the individual has been convicted of a serious crime against life, personal integrity, liberty, sexual freedom or integrity, terrorism, or any other serious crime that poses a serious risk to life, health, or physical integrity, and the circumstances of the crime, criminal record, assessment of their personality, or other available information indicate a significant risk of recidivism, the judge or court may order the taking of biological samples and the performance of analyses to obtain DNA identifiers and their registration in the police database. Only analyses necessary to obtain identifiers that exclusively provide genetic information revealing the person's identity and sex may be carried out.

If the individual objects to the collection of the samples, their collection may be enforced through the use of the minimum coercive measures necessary for their execution, which must in all cases be proportionate to the circumstances of the case and respectful of their dignity.