Four outs away from another defeat — one that would be pinned on a lack of hitting, especially in the clutch — the Yankees’ bats woke up from a summer slumber.

Aaron Judge’s two-run bolt to right off Colin Poche pushed the Yankees ahead of the Rays in the eighth and four batters later the slumping Didi Gregorius crushed another Poche fastball for a grand slam to right and helped solidify a much-needed 8-3 victory in front of a sweaty Yankee Stadium crowd of 40,401.

The late-inning thunder was accompanied by two innings of shutout relief by right-hander David Hale and increased the Yankees’ AL East lead over the Rays to six games.

While Aaron Boone didn’t believe CC Sabathia and Avisail Garcia’s shouting match after Garcia was called out on strikes to end the sixth lit a match under his team’s dead bats, others said they fed off the veteran lefty even if he said he wasn’t yapping at Garcia.

“A little bit, CC is our leader. We always have his back,’’ said Judge, who drove a 95 mph fastball down and away into the right-field seats for his 10th homer and first since hitting two on July 5. “We feed off that. It fired us up a little bit.’’

Gregorius took extra pride in hitting it off Poche.

“It felt good coming through for the team,’’ said Gregorius, who kept Sabathia from getting close to Garcia while they exchanged words. “Especially against a left-hander.’’

Going back to his final two at-bats on July 4 and including his first two at-bats Tuesday night Gregorius was in a 1-for-25 funk.

“Any hit at all after not getting any at all,’’ said Gregorius, who started an inning-ending, 6-4-3 double play with the bases loaded in the seventh after Hale replaced Nestor Cortes Jr. with one out and the bases jammed.

Having scored 17 runs in the previous six games — four losses — the Yankees also received homers from Edwin Encarnacion (third in two games) and DJ LeMahieu, whose solo homer in the sixth pulled the Yankees to within 3-2.

Gone after six innings in which Sabathia allowed three runs and five hits (two homers), the lefty said he had a feeling he gave the Yankees a chance to win.

“Try to keep the game as close as possible. I knew we were going to score runs,’’ said Sabathia, who watched left fielder Brett Gardner rob Garcia of a homer with a leaping catch to end the fourth after Yandy Diaz had homered to give the Rays a 2-1 lead.

When Gregorius came to the plate in the eighth the Yankees led, 4-3, but had one at-bat with a runner in scoring position and were in a 4-for-45 (.089) funk in the clutch. Down in the count, Gregorius allowed the Yankees to exhale. All that was left was to get three outs in the ninth to seal the victory, which All-Star closer Aroldis Chapman failed to do Monday night.

After getting Monday night’s hero Travis d’Arnaud to hit into the double play that ended the seventh, Hale worked a clean eighth but when two of the first three Rays reached base in the ninth, Boone called for Zack Britton and he whiffed the final two batters to cement the victory.

A loss would have shaved the Yankees’ lead over the Rays to four. That would have been the Yankees’ slimmest margin since June 19 when they had a 3 ¹/₂-length lead.

Thanks to the bats wiping the sleep from their barrels that didn’t happen.