WASHINGTON — President Obama announced Thursday he is ending a longstanding immigration policy that allows any Cuban who makes it to U.S. soil to stay and become a legal resident.

The repeal of the “wet foot, dry foot” policy is effective immediately. The decision follows months of negotiations focused in part on getting Cuba to agree to take back people who had arrived in the U.S.

“Effective immediately, Cuban nationals who attempt to enter the United States illegally and do not qualify for humanitarian relief will be subject to removal, consistent with U.S. law and enforcement priorities,” Obama said in a statement. “By taking this step, we are treating Cuban migrants the same way we treat migrants from other countries. The Cuban government has agreed to accept the return of Cuban nationals who have been ordered removed, just as it has been accepting the return of migrants interdicted at sea.”

Obama is using an administrative rule change to end the policy. Donald Trump could undo that rule after becoming president next week. He has criticized Obama’s moves to improve relations with Cuba. But ending a policy that has allowed hundreds of thousands of people to come to the United States without a visa also aligns with Trump’s commitment to tough immigration policies.

President Clinton created the “wet foot, dry foot” policy in 1995 as a revision of a more liberal immigration policy that allowed Cubans caught at sea to come to the United States and become legal residents in a year.

The two governments have been negotiating an end to “wet foot, dry foot” for months and finalized an agreement Thursday. A decades-old U.S. economic embargo, though, remains in place, as does the Cuban Adjustment Act, which lets Cubans become permanent residents a year after legally arriving in the U.S.

Under terms of the agreement, Cuba has agreed to take back those turned away from the U.S., if the time between their departure from Cuba and the start of deportation hearings in the U.S. is four years or less. Officials said the timeframe is required under a Cuban law enacted after Congress passed the Cuban Adjustment Act.