The perfect game disappeared with two outs in the sixth and the shutout went kaput with two outs in the seventh.

And when it comes to blemishes on Masahiro Tanaka’s ledger Friday night, that was about all.

Turning in one of the best performances by any Yankees pitcher this season, Tanaka allowed two hits and struck out a career-high 14 in a 6-1 victory over the Rays in front of 40,470 at the Stadium.

The victory moved the Yankees (55-46), who have won five straight and seven of their last eight, into first place in the AL East, a half-game in front of the Red Sox.

After holding a four-game lead on the Red Sox on June 12, the Yankees went 10-22 to fall 4 1⁄2 games behind Boston, but they have picked up five games on the Red Sox in the last nine days. It’s the first time they have been atop the division since June 28.

Tanaka struck out the first five he faced and seven of the first nine. “Incredible,” said Aaron Judge, who hit his 33rd homer. “Every one of his pitches was working well. That’s what you expect out of your ace.”

advertisement | advertise on newsday

This season, however, Tanaka (8-9, 5.09 ERA), seldom has resembled the pitcher he was in the previous three years. He pitched like an ace Friday night, though, improving to 3-2 with a 3.06 ERA in his last seven starts.

“He threw a brilliant game tonight,” Joe Girardi said. “You want to get him on a roll because he’s really important to us.”

Tanaka was backed by home runs from the starting outfield of Brett Gardner, Judge and Clint Frazier, who hit a three-run shot.

Yankees videos

Gardner, who won Thursday night’s game with a walk-off homer in the 11th, led off the first inning against Austin Pruitt (5-2, 6.63) with his 19th homer, second-most on the Yankees. Judge destroyed a fastball to left in the fourth to make it 2-0 and Frazier hit a drive estimated at 455 feet into the bleachers above the Rays’ bullpen in the fifth. The rookie’s fourth homer made it 5-0.

“It was awesome,” Frazier said of the outfield’s production. “Before the game, Gardy was talking about how he was about to catch Aaron in home runs and he hit that leadoff home run. Judge had to put him to sleep right after that and just add on to the million he’s already got.”

Much of the crowd, and probably quite a few Yankees as well, thought they might see some history. Tanaka took a perfect game into the sixth and retired the first two batters in the inning. But No. 9 hitter Adeiny Hechavarria swatted a first-pitch splitter back up the middle for the Rays’ first hit.

“I realized after the fifth inning I hadn’t given up a hit,” Tanaka said through his translator. “And then I went back out in the sixth and I gave up the hit, so that was that.”

The shutout was intact until Lucas Duda, acquired from the Mets the day before, homered into the second deck in right with two outs in the seventh.

advertisement | advertise on newsday

It was an especially impressive effort, given what the Rays (53-51) had done in two previous outings against Tanaka. They scored seven runs in the season opener and tattooed him for six runs and nine hits, including three homers, on May 20.

David Robertson pitched a scoreless ninth to polish off the 2-hour, 23-minute game.

With a devastating splitter and slider, his two most important pitches, Tanaka struck out the side in his first inning and his last. After striking out Tim Beckham with his 109th and final pitch to end the eighth, he received a thunderous ovation.

Did he hear the fans’ ovation? “Loud and clear,” Tanaka said with a smile. “Hopefully I was able to entertain everybody.”

Yankees starter Masahiro Tanaka walks to the dugout after an inning against the Tampa Bay Rays at Yankee Stadium on Friday, July 28, 2017. Photo Credit: Jim McIsaac