SOON after Oct. 7, when Israel suffered the worst attack on Jews since the Holocaust, US President Joe Biden gave the nation’s leaders some remarkably good and personally chastening advice: Don’t let rage drive you to the mistakes the US made after a terrorist attack on America killed almost 3,000 people on Sept. 11, 2001.

So far, Prime Minister Benjamin Netanyahu has ignored that counsel. As the civilian death toll from Israel’s invasion of Gaza soars — above 13,000, according to Gaza’s Hamas-run government — the actions of the Israel Defense Forces (IDF) face rapidly rising levels of opposition and outrage around the world. It now risks learning the hard lesson that America’s “shock and awe’’ invasions of Afghanistan and Iraq taught the US: Winning on the battlefield can lose you the war.

As I’ve written, Hamas shares full responsibility for current Palestinian suffering, as it was an essential part of the tactics behind its Oct. 7 attack, a terrorist spectacular that was more provocative than 9/11 in terms of both its per capita death toll and deeply personal barbarity. Yet that neither exonerates Israel in moral or legal terms, nor helps it strategically.

On Oct. 26, the United Nations General Assembly voted by 121 nations to 14, with 45 abstentions, for a resolution that failed to condemn Hamas and called for a sustained and durable truce in Gaza. Such a ceasefire would halt the Israeli invasion and risk leaving Hamas in place, intact, and with its status as the primary representative of the Palestinian people enhanced. Small wonder that Israel has resisted the idea, but now even the US — Israel’s critical supporter — has abstained in a vote to enable humanitarian pauses in Gaza, and it has threatened to sanction extremist Jewish settlers.

This international pressure will only grow the more it appears Israel exaggerated its claims of Hamas using civilians as human shields — claims the IDF used to justify the high rate of collateral damage and casualties caused by its invasion. A week after taking control of Gaza’s main al-Shifa hospital, the finds that the IDF say it’s made — a few aging assault rifles, a tunnel, and CCTV footage of one wounded and one healthy hostage brought to the medical facility on Oct. 7 — fall short of earlier claims that Hamas was using al-Shifa as a major command center and military hub. Israel may yet find more evidence to support this; the complex is large. But the IDF increasingly risks suffering the fate of the US military in Iraq, sent by politicians to find weapons of mass destruction that weren’t there.

Combined with Washington’s deceit and lack of coherent exit strategies, the disproportionate nature of the US response to 9/11 quickly emptied the well of international sympathy it had gained. The two decades of war and tens of thousands of Afghan and Iraqi casualties that followed spawned new terrorist threats and left the US geopolitically weakened. US coffers were sapped, alliances strained, strategic focus was diverted from Beijing and Moscow, and American credibility and soft power were incinerated.

This was the backdrop to Biden’s advice to Israel, and he can’t have given it lightly in such a public forum. As chair of the Senate Foreign Relations Committee at the time, Biden was not only a vocal advocate for the US response to 9/11, but also a key enabler of the Iraq war, as the Senate hearings he oversaw helped shape support for the invasion. It took two years for him to stop defending that decision, as the initial US military victory soured. It also fell to Biden to conduct a humiliating US withdrawal from Afghanistan that left the Taliban back in power after two decades of war aimed at keeping them out.

It isn’t too late for Israel’s government to understand that it has walked into a trap. Recent indications that the IDF plans to turn south and attack in areas of Gaza where it evacuated civilians only weeks ago underscore that. Israel now believes that Hamas commanders moved south from Gaza City before the IDF sealed it off, suggesting that like the Taliban in Afghanistan in 2001, Hamas may have withdrawn a substantial part of its forces in advance to ensure it can fight another day.

There are no good or easy options for Israel. Yet Netanyahu has a chance to rescue his strategy by embracing a deal, reportedly close to agreement, to swap about 50 of the 239 hostages Hamas is believed to have taken on Oct. 7. In exchange, Israel would give up jailed Palestinians and allow a humanitarian pause in the fighting of several days. That would create at least the opportunity to start turning the tables on Hamas’ nihilistic goals and shift international focus from Israel’s killing of Palestinian civilians to Hamas’ refusal to give up all the hostages.

Israel should not be afraid if that leads to a longer ceasefire, because the IDF would remain in place and defeating Hamas requires more than killing its fighters. To win, Israel needs to make the group’s ideology — one that offers Israel’s destruction as the only viable future for Palestinians — redundant.

Netanyahu already hinted at one alternative end-state for Gaza, in the form of “indefinite” military occupation by Israel. Some of his more extreme cabinet ministers have hinted at another: the expulsion of Palestinians from Gaza, an act that no matter the means or rationalization would amount to ethnic cleansing. Both alternative futures are morally and legally unacceptable.

Just as important, neither offers long-term security for Israel, because as the current conflict shows, the Palestinian question is not just a domestic issue, but one that has the power to draw the entire Middle East into its vortex, amid a balance of power that may not always favor Israel. Both of these paths would put the Jewish state at greater risk than anything Hamas has the power to do.

BLOOMBERG OPINION