OAKLAND — A federal judge has reversed his order requiring the government to preserve records related to the National Security Agency’s surveillance of online communications, heeding warnings that the ruling could force a shutdown of the program and imperil national security.

At an emergency hearing Friday afternoon, U.S. District Judge Jeffrey White of the Northern District of California ruled from the bench that the government may continue purging old records collected under Section 702 of the Foreign Intelligence Surveillance Act, which allows the NSA to scrutinize foreigners’ email and social media accounts. White had ordered the government to maintain the records on Thursday after the Electronic Frontier Foundation argued that they were important evidence in civil suits challenging the NSA’s dragnet surveillance. But he changed course after the government cautioned that it may have to suspend the program if old data could not be cleared.