President Trump on Friday defended his decision to pull US forces out of Syria and Afghanistan — a day after the Republican-led Senate voted to advance an amendment opposing plans for any abrupt troop withdrawal.

“I inherited a total mess in Syria and Afghanistan, the ‘Endless Wars’ of unlimited spending and death. During my campaign I said, very strongly, that these wars must finally end,” the president said in a tweet.

“We spend $50 Billion a year in Afghanistan and have hit them so hard that we are now talking peace after 18 long years. Syria was loaded with ISIS until I came along,” he continued.

“We will soon have destroyed 100% of the Caliphate, but will be watching them closely. It is now time to start coming home and, after many years, spending our money wisely. Certain people must get smart!” he said.

In a bipartisan rebuke to Trump, Senate Republicans and Democrats voted Thursday in favor of a nonbinding amendment drafted by Majority Leader Mitch McConnell (R-Ky.) expressing strong opposition to the president’s plans to withdraw US troops.

The measure said ISIS and al Qaeda militants still pose a serious threat to the US, and warned that “a precipitous withdrawal” of American forces from those countries could “allow terrorists to regroup, destabilize critical regions and create vacuums that could be filled by Iran or Russia.”

In December, Trump abruptly tweeted plans for a pullout from Syria, arguing that ISIS had been defeated, even though his intelligence chiefs have said the group remains a threat.

His announcement on Syria prompted the resignation of Defense Secretary James Mattis.

Trump also ordered the military to develop plans to remove up to half of the 14,000 American forces in Afghanistan.

This week, US intelligence chiefs publicly broke with Trump, contradicting several of his foreign policy positions by telling senators that ISIS has not been defeated, Iran is probably not producing nukes and North Korea is unlikely to abandon its nuclear weapons program.

In a Twitter broadside, Trump later ripped the intelligence community, calling the intel officials “extremely passive and naive when it comes to the dangers of Iran.”

“Perhaps Intelligence should go back to school!” he railed.

But on Thursday, Trump claimed his intelligence chiefs — including Director of National Intelligence Dan Coats and CIA Director Gina Haspel — told him that they were misquoted by the press, although their testimony was aired live.