Luis Enrique hugged his backroom staff in relief more than joy after Spain needed penalties to squeeze past Switzerland’s 10 men into the semi-finals.
Spain faded after storming into an early lead and missed a host of chances to win the tie in extra-time.
They were nearly embarrassed by Switzerland, who played 73 minutes with 10 men after Remo Freuler was sent off by Michael Oliver.
Spain needed goalkeeper Unai Simon, who dropped a real clanger against Croatia in the previous round, to steer them through the shoot-out as he saved from Fabian Schar and Manuel Akanji.
Switzerland’s nerve failed them when they needed it most and they missed three of their four spot-kicks after converting all five in their famous win over France in the round of last 16.
Spain produced a real Jekyll and Hyde display and they will have to be more consistent in Tuesday’s first semi-final at Wembley if they are to maintain their quest for a first Euro title since 2012.
Switzerland’s odyssey, which has seen them clock up 10,571 miles travelling to Baku twice, Rome and Budapest, finally ended in Saint Petersburg.
As well as all those air miles they have amassed much credit in reaching their first quarter-final since 1954.
Spain took the lead after just eight minutes when Koke’s deep corner fell to Jordi Alba and the Barcelona left-back’s shot came off Denis Zakaria’s outstretched leg and past Yann Sommer.
Chelsea skipper Cesar Azpilicueta should have made it 2-0 when he missed the target from a free header.
The mountain Switzerland had to climb became even taller when the lively Breel Embolo went off injured.
But the Swiss are good mountaineers and they battled back as Spain took the foot off the pedal following their flying start.
Xherdan Shaqiri was a threat with his corners and Spain struggled to deal with his precise deliveries.
But Switzerland failed to capitalise as first Nico Elvedi headed over and then Silvan Widmer headed over.
Switzerland grew in confidence and Zakaria was just inches away from equalising when he headed just wide of the far post.
Simon then denied Steven Zuber before Shaqiri deservedly equalised on 68 minutes following a mix-up in the Spanish defence.
Aymeric Laporte and Pau Torres got in a tangle and Freuler fed Shaqiri, who gleefully slotted home.
The game changed again nine minutes later when Oliver showed Freuler a straight red card for his challenge on Gerard Moreno.
Switzerland protested, but Freuler’s feet were off the ground and Oliver deemed his challenge was reckless.
Spain cranked up the pressure in extra-time on Switzerland’s 10 men and Moreno missed with a shot and a header when he should have scored.
When they did hit the target, Sommer stood tall and the Swiss goalkeeper saved from Moreno, Alba, Olmo and Mikel Oyarzabal.
Ricardo Rodriguez was just as defiant and he threw himself in front of Marcos Llorente’s shot to make an incredible block and earn the Swiss a shoot-out.