Retiring Republican Sen. Jeff Flake rose to Sen. Chuck Schumer’s defense Wednesday after President Trump blasted the New York Democrat over an immigration program reportedly used by the terrorist who attacked pedestrians in Manhattan.

Flake, who announced last month that he would not seek re-election, responded to Trump’s tweet referring to the diversity visa lottery program that news reports said enabled Uzbek native Sayfullo Saipov to enter the country as a “Chuck Schumer beauty.”

The Arizona Republican said the immigration reforms passed by the bipartisan “Gang of Eight,” which he and Schumer were part of, would have scrapped the program.

“Actually, the Gang of 8, including @SenSchumer, did away with the Diversity Visa Program as part of broader reforms. I know, I was there,” Flake, an outspoken critic of Trump, wrote on Twitter. “In fact, had the Senate Gang of 8 bill passed the House, it would have ended the Visa Lottery Program AND increased merit based visas.”

The senators — four Democrats and four Republicans — wrote an immigration reform bill in 2013 that would have done away with the diversity program.

The bill passed the Senate 68-32 but it never reached President Obama’s desk for signature because then-House Speak John Boehner never allowed it to reach the floor for a vote over opposition from Republicans.

Schumer also shot back at Trump’s tweet and accused the president of politicizing the terror attack that killed eight people in lower Manhattan.

“I have always believed and continue to believe that immigration is good for America. President Trump, instead of politicizing and dividing America, which he always seems to do at times of national tragedy, should be focusing on the real solution — anti-terrorism funding — which he proposed cutting in his most recent budget,” Schumer said in a statement.

The president called for an end to the program in his tweet.

“The terrorist came into our country through what is called the ‘Diversity Visa Lottery Program,’ a Chuck Schumer beauty. I want merit based,” he posted. “We are fighting hard for Merit Based immigration, no more Democrat Lottery Systems. We must get MUCH tougher (and smarter). @foxandfriends.”