Pablo Zabaleta was West Ham's unlikely saviour as they battled past League One Gillingham in the FA Cup third round.
The 34-year-old, only on the field as a substitute after an injury to Ryan Fredericks, scored his first goal in almost three years to break the deadlock midway through the second half.
Then, as Gillingham chased an equaliser, fellow sub Pablo Fornals finished them off with a breakaway goal in stoppage time to seal a 2-0 win.
Gillingham threatened an upset as West Ham failed to get to grips with the surface or their opponents during the first half, but they were a different side in the second to ensure they avoided a giant-killing after defeats to League One sides Wigan, AFC Wimbledon and Oxford in recent years.
David Moyes, who was in charge when they crashed at Wigan in 2018, was desperate to avoid further humiliation in only the second match since he was parachuted back in as manager and named his strongest available side.
It may have been the most expensively-assembled team to run out at the Priestfield Stadium but reputations counted for little in the opening stages as Gillingham, unbeaten in their last seven matches, refused to allow the visitors to settle.
Connor Ogilvie had Lukasz Fabianski scrambling with a long-range effort before the Hammers keeper had to make a point-blank save to deny Brandon Hanlon from one of a series of Gillingham corners.
West Ham did not manage a meaningful effort on goal during a tough first half which got worse shortly before half-time when wing-back Fredericks pulled up mid-sprint with a hamstring injury.
Moments after the interval the chance West Ham had been waiting for arrived, but £45million record buy Sebastien Haller failed to take it.
The French striker raced onto Felipe Anderson's flick and charged clean through, but as goalkeeper Jack Bonham came out Haller shanked his finish high over the crossbar.
Haller was bearing down on goal again in the 50th minute, but this time his angled snap-shot hit the inside of the near post and spun away.
Nevertheless, the change in the sense of urgency from West Ham was palpable.
Gills boss Steve Evans said beforehand that he had fancied his chances of a famous win when Manuel Pellegrini was still manager but the arrival of Moyes – a friend from their time together as youngsters at Celtic Boys Club – had made him think again.
Pellegrini's West Ham may well have rolled over but Moyes' side already seem to be made of sterner stuff and the goal eventually arrived in the 73rd minute, Anderson freeing Arthur Masuaku to reach the byline and pull the ball back.
The ball had too much pace for Fornals but it skidded through to Zabaleta, whose shot took a deflection on its way past Bonham.
It was the veteran Argentine's first goal for West Ham, and his first since scoring in a 5-1 FA Cup win for Manchester City against Huddersfield in March 2017.
Gillingham mounted a late rally, but they were caught on the break at the death when Anderson teed up Fornals to leave the home defenders on their knees.