The National Security Agency on Sunday will end its mass collection of data about Americans’ phone calls, 14 years after the counterterrorism program began in secret under the authority of President George W. Bush.

The halt was ordered by Congress, which in June passed the USA Freedom Act to ban the controversial collection of information known as metadata. That data includes the dates and durations of phone calls and logs of call times, but not content.

Under the new law, the NSA must obtain a court order to serve on the phone companies for every phone number or account it wants information on.

President Obama in January 2014 called on Congress to come up with a way to end the bulk collection of phone metadata, saying that although he had seen no evidence of abuse by the NSA, the program lessened trust in the government.

The government had kept the program mostly secret for years. But in the summer of 2013 it was forced to acknowledge it following the leak of a court order by former NSA contractor Edward Snowden showing that the agency was gathering from a Verizon phone company “all call detail records” of its customers on a daily basis.

The revelation touched off a contentious two-year debate about the proper scope of government surveillance.

Although the phone metadata program had been placed under court supervision in 2006, that action, too, was secret. The public – and many lawmakers, apparently – did not know the vast scope of the collection or the legal authority it was based on.

Following the leak, the government revealed it had interpreted Section 215 of the Patriot Act in a way that allowed such bulk collection. The reasoning essentially held that all records were needed in the event that one day some of them might prove useful in foiling a terrorist plot.