BRUSSELS — A top court on Tuesday struck down a European Union law, adopted in response to deadly terrorist attacks, that required telecommunications companies to retain information about calls and emails for up to two years.

The European Union passed the legislation in 2006 after bombings on the mass transit systems in London and Madrid, with the goal of aiding security forces in tracking those suspected of terrorism and other serious crimes. The retained data typically indicates the people who were involved in a communication, where they were and how often they communicated, but it does not reveal the content of the conversations or messages.

However, the law “exceeded the limits” of proportionality, according to the European Court of Justice, whose headquarters are in Luxembourg.

Privacy advocates in Ireland and Austria had pressed their home governments to pursue the case, which gained urgency in light of disclosures in the last year of widespread electronic surveillance in Europe by the United States’ National Security Agency.