But Mr. Trump also claimed that discussing a potential purchase of Greenland, a semiautonomous territory reliant on Danish support, was not the purpose of his trip to Denmark. “We may be going to Denmark, but not for this reason at all,” he said, and claimed again over the weekend that the purchase was not “No. 1 on the burner.” He also expressed hesitation about the trip, noting that “I’m supposed to be going there.”

On Tuesday night, Mr. Trump scrapped the entire trip.

“At this time, the visit to Denmark is canceled,” said Judd Deere, a White House spokesman.

Mr. Trump said he was reacting to Ms. Frederiksen’s hard rejection of his interest. “Greenland is not for sale,” she told a Danish newspaper this week. “Greenland is not Danish. Greenland belongs to Greenland. I strongly hope that this is not meant seriously.”

Apparently, it was.

“The Prime Minister was able to save a great deal of expense and effort for both the United States and Denmark by being so direct,” Mr. Trump tweeted. “I thank her for that and look forward to rescheduling sometime in the future!”

Mr. Trump has been enjoying the attention generated by his once-secret interest in buying Greenland. In a rare moment of demonstrating that he is sometimes in on the joke, the president tweeted a photograph on Monday of a gold skyscraper with the Trump logo emblazoned on the top, plopped in the middle of an otherwise pastoral arctic setting.