When selling a primary residence, the outstanding balance of the mortgage may be subtracted from the sale price to calculate the capital gain, provided that: The property sold has been the taxpayer’s habitual residence (lived in for ≥ 3 years or with justified reasons for the change). The mortgage was linked to the purchase of that property (not loans for other purposes). This is not a deduction as such, but it reduces the profit and, therefore, the income tax (IRPF) payable.