The transaction with CaixaBank involves 262 branches and 2,400 staff in retail banking, wealth and investment management and corporate banking.
The UK banking giant said the deal, which is expected to complete by the end of the year, will generate an estimated loss of £500 million compared with the book value.
However, chief executive Antony Jenkins said the sale represented progress in the company's strategy announced in May to offload non-core assets.
He added: "We remain on track to rebalance Barclays as part of our strategy to deliver sustainable returns for our shareholders."
Barclays' investment bank and Barclaycard operations in Spain are not part of the agreement with Caixa, which has 13.6 million customers, 31,500 staff and more than 5,500 branches.