The sale marks one of the first big steps in Barclays' bid to exit most of its European retail banking businesses, after Chief Executive Antony Jenkins embarked on a turnaround plan earlier this year to try and lift profitability.
Barclays will make thousands of job cuts in the next three years as part of the restructuring and has said it wants to refocus on its British and African businesses, credit cards and investment banking in Britain and the United States.
Read MoreBarclays axeswinging over investment bank jobs
It has been gradually selling off units it now considers non-core - including its United Arab Emirates retail banking business, sold to Abu Dhabi Islamic Bank in a deal also completed on Sunday - and began sounding out potential buyers for parts of its Spanish business in May this year.
"We remain on track to rebalance Barclays," Jenkins said in a statement on the sale.
Barclays will make a loss after tax on the sale of about 500 million pounds, with 400 million pounds of that to be reported in the third quarter of 2014 and the rest on completion of the deal, it said.
The British bank expanded quickly in Spain in the early 2000s, but a prolonged recession brought losses on corporate loans and it had to close dozens of bank offices and cut jobs.
As the country returns to growth and Spanish banks' fortunes start to improve after they suffered steep losses from a 2008 property crash, some international lenders such as Citigroup are also jumping at a chance to sell their retail banks.
Barclays parked its retail banking operations in Spain, Italy, France and Portugal in a "bad bank" earlier this year so they could be sold, separated or floated.
Its European retail business has lost almost 2 billion pounds ($3.4 billion) over the last four years.
Mergers in Spain
Spanish banks are now focusing on trying to ramp up revenues gutted during the prolonged economic downturn, including through winning over more depositors and borrowers.
Caixabank, which bought several ailing savings banks bailed out by the government after the real estate slump, will be taking on a 270 branch network and nearly 2,400 from Barclays, as well as 550,000 retail and private banking clients.
Read MoreBarclays profits hit by investment bank problem
The Barcelona-based lender recently missed out on a chance to buy a local rival, state-owned Catalunya Banc, which was snapped up by BBVA in an auction.
Barclays will keep its investment banking division in the country, and the deal with Caixabank does not include Barclays' credit card operations in Spain, the banks' announcements said.
Caixabank said the final price it would pay for the assets would depend on the net asset value of Barclays Bank in Spain by the end of the year, when the transaction is due to close.
The announced price was set on a value of the whole business of 1.7 billion euros as a whole, it said.