Joe Lieberman represented Connecticut in the U.S. Senate from 1989 to 2013 and retired Gen. Jack Keane is former vice chief of staff for the U.S. Army. Both serve on the board of directors at the Institute for the Study of War, whose analysts contributed to this article.

President Trump is considering whether the United States should withdraw all of its troops from Afghanistan this year. Doing so would be a humiliating surrender to an enemy we have already largely defeated. It would also be a disastrous error.

The U.S. military is in Afghanistan today fighting deadly enemies who continue to plan and prepare to kill Americans at home. It is not policing Afghanistan. It is not helping Afghans build their nation. It is performing missions only the U.S. armed forces can perform. If U.S. troops are withdrawn prematurely it is Americans, not just Afghans, who will suffer the consequences.

The United States entered Afghanistan in 2001 to defeat and expel al-Qaeda from the bases it used to launch the Sept. 11 attacks and to prevent the terror group from returning. We accomplished that aim in 2002 and have been struggling since then to find a way to leave Afghanistan without allowing al-Qaeda, and now the Islamic State, to regain an important safe haven.

There have been many errors and setbacks in that effort. But the force reductions that the president has already ordered will bring the number of U.S. troops in Afghanistan below 10,000 this year. That is one-tenth the number who were there at the height of the U.S. war effort. Conducting a complicated withdrawal within the next few weeks entails extremely high risks and would likely result in unnecessary American casualties.

U.S. forces that remain in Afghanistan today are carrying out a clear and limited mission. They are not engaged in counterinsurgency. They are not engaged in nation-building. They are not police. They are no longer primarily supporting Afghan forces fighting the Taliban. They are instead laser-focused on fighting al-Qaeda and the Islamic State in ways that only the U.S. military can.

Afghanistan has become not a quagmire from which the United States must extricate itself, but rather a critical platform that the United States needs to keep deadly enemies off balance. The terror attack in Pensacola, Fla., in December was planned and supported by al-Qaeda leaders enjoying a partial safe haven in Yemen. Its aim and effects were limited in part because the United States has continued to target and kill leaders and key facilitators of that group in Yemen. It strongly suggests that, should terrorist groups regain a safe haven in Afghanistan as in Yemen, they will use it to plan and conduct attacks against the American homeland.

Trump promises to attack terrorists in Afghanistan if they threaten or attack Americans — even after U.S. forces leave, likely having the model of Yemen in mind. But Afghanistan is not Yemen. U.S. forces can hit targets in Yemen, Somalia, Syria and Libya without keeping large numbers of troops on the ground because those countries all have long coastlines. Afghanistan is landlocked. The most dangerous areas are more than 700 miles from the ocean, deep in valleys sheltered by mountains many thousands of feet high. U.S. forces can hit terror groups in Afghanistan only from bases inside Afghanistan. Giving up those bases means giving up much-needed and largely successful operations against those groups.

The costs of returning U.S. forces to Afghanistan once they leave are prohibitive, much more so than reentering Iraq after the catastrophic rise of the Islamic State. In 2014, the United States relied on logistical infrastructure and long-term basing in neighboring Kuwait; in contrast, the United States relies on precarious and sometimes threatened supply lines through Pakistan, where it lacks a base, to reach Afghanistan. Withdrawing from Afghanistan is likely irreversible.

The United States can afford the cost of fighting al-Qaeda in Afghanistan. The few thousand troops needed in Afghanistan represent a small fraction of the U.S. military and only a tiny fraction of the defense budget — to say nothing of the federal budget. U.S. commanders have recommended a residual presence even smaller than the United States now has. This small number of troops is a wise investment in protecting Americans against al-Qaeda. But withdrawing all troops dangerously exposes the American people. U.S. casualties in Afghanistan have been and will remain extremely low. And while we regret and mourn every one lost, we know that our servicemen and women (and their civilian counterparts) volunteer to risk their lives to defend their nation against deadly threats — and that they are prevailing against those threats in Afghanistan. They will not be grateful to have their sacrifices squandered.

Mr. President, please do not impose an unnecessary defeat on U.S. forces, engage in a humiliating surrender, and retreat from an important front against some of our deadliest foes. You are right that we cannot and should not remain in Afghanistan forever simply to help Afghans build their nation. That is not why we went there, and it is not why we are there today.

But we can and should remain there as long as we need to, at the small force levels required, to protect Americans from those who would kill us. You have rightly made that the mission of our troops in Afghanistan and everywhere they are deployed. Let them carry out that mission.

Read more: