A guards walks into the maximum-security detention center at Guantanamo Bay, Cuba, in October 2016. (John Moore/Getty Images)

HAVING PROMISED to close the Guantanamo Bay prison during his first year in office, President Obama will leave about 40 prisoners there when he completes his second term. It’s a failure that has something to do with Mr. Obama’s reluctance to press his legal authority and something to do with the sluggishness of his appointees at the Pentagon; but it’s mostly the result of obstructionism by Congress, which foolishly blocked the transfer of detainees to prisons in the United States and placed onerous conditions on their release to other countries. Given the difficulties he faced, Mr. Obama has done the country a service by greatly reducing the blight that Guantanamo placed on the country’s international reputation. Now he must hope that Donald Trump does not give it a new life.

As President George W. Bush seemed to recognize by the end of his tenure, the warehousing of hundreds of foreign terrorism suspects on the U.S. base in Cuba was a tragic mistake that did far more harm than good to the cause of counterterrorism. The mistreatment of some detainees alienated even close U.S. allies and served as a recruitment tool for al-Qaeda and the Islamic State, which invariably parade their Western detainees in orange jumpsuits like those Guantanamo made famous. Mr. Bush whittled the detainee population from more than 700 to 242 before leaving office and established systems for reviewing the prisoners’ cases and trying those accused of war crimes before military commissions.

Mr. Obama’s contribution has been to find new homes for all but three or four of those cleared for release — some of whom have remained in Guantanamo years after they were judged to be eligible for transfer. Those who remain include 10 who were convicted or are being tried by the commissions, and two dozen who cannot be tried and are considered too dangerous to release. As importantly, Mr. Obama demonstrated that it is possible to wage war against international terrorist groups without resorting to Guantanamo; on his watch, suspects have been interrogated on ships or in other extraterritorial locations, but none have been sent to Guantanamo.

U.S. prosecutors have demonstrated also that it is possible to efficiently try, convict and imprison those accused of terrorist acts in the federal court system. Meanwhile, the flawed military commissions in Guantanamo have grossly delayed justice, especially for the ringleaders of the 9/11 attacks; as The Post’s Missy Ryan and Julie Tate have reported, their trials may not get underway until 2020. For al-Qaeda chieftains, run-of-the-mill militants and even swept-up innocents, Guantanamo has a one-way door. Once they’re inside, it becomes extraordinarily difficult to try the guilty or release those who are no longer a threat.

Having loosely promised during his campaign to “load [Guantanamo] up with some bad dudes,” Mr. Trump ought to carefully consider this history. If his desire is to try and convict captured terrorists, U.S. courts have proved to be the best venue, and federal supermax prisons have had no trouble holding those convicted. If he wishes to avoid handing easy propaganda victories to enemies of the United States, Mr. Trump will not send new prisoners to Guantanamo, but instead finish Mr. Obama’s work and shut it down.