Donald Trump made clear on the campaign trail that he intends to “get rid of” the Environmental Protection Agency and many of its Obama-era regulations.

And in its first six months, his administration has overturned or halted nearly two dozen environmental policies and significantly backed off enforcement of environmental pollution laws. EPA Administrator Scott Pruitt, who sued the EPA 14 times in his previous post as attorney general of Oklahoma, has been leading the charge.

But rolling back Obama-era environmental protections is actually not that easy. There are laws that govern how the EPA can change its policies. And the courts are proving to be a considerable obstacle to the Trump agenda.

In June, for example, the EPA announced plans to delay implementation of a 2015 regulation to reduce smog-forming pollution from smokestacks and tailpipes. But then last week, 16 state attorneys general sued the EPA over the delay. A day after the lawsuit was filed, the EPA reversed its earlier announcement and ditched the delay.

“It's unprecedented for the head of the Environmental Protection Agency to act with such reckless disregard for the clean air and the clean water laws that he's entrusted to administer, and it's not surprising that actions that are taken with such lawlessness are being [challenged] in court,” Vicki Patton, general counsel for the Environmental Defense Fund, told me.

Several of Pruitt's EPA actions have already been overturned

Other instances when lawsuits have successfully stopped Pruitt’s EPA from reversing environmental regulations include:

In June, the EPA restored a mercury protection rule after being sued by the Natural Resources Defense Council (NRDC). They sued after the EPA withdrew the protection in January at the request of the new White House.

In July, a three-judge panel of a federal appeals court struck down the EPA’s suspension of a 2016 rule that limits methane emissions from oil and gas wells. (The full circuit court reaffirmed the panel’s order last week.)

Also in July, a federal appeals court rejected the EPA’s justification for lowering renewable fuel standards.

Jody Freeman, Harvard law professor and former climate adviser to President Barack Obama, forewarned in a January Vox interview with Brad Plumer that the Trump administration would face legal obstacles to its environmental deregulation agenda. One of the biggest obstacles, she explained, to revoking or relaxing existing rules is the Administrative Procedure Act’s notice and comment process:

...to change a rule, by law, the agency has to do a public comment period — that’s typically something like 60 days, though it can be up to 120 days. Then the agency has to take time to consider and respond to all the public comments on the proposed rule and develop a record that shows they thought carefully about them. If they try to shortchange this process and rush out a brand new rule, it really will not go well for them when they get into court. The procedural checks the agency has to go through are very important, and a court will invalidate a rule that wasn’t done correctly.

And there are more, ongoing court challenges to suspensions of Obama-era environmental policies

There are many more open lawsuits against Pruitt’s EPA over a variety of issues, some of which relate to similar, allegedly unfounded, suspensions of Obama-era policies:

In April, the NRDC and the Pesticide Action Network sued the EPA over its decision to discontinue efforts to ban a certain insecticide associated with negative health risks. Five states and the District of Columbia have since joined that case against the EPA over the controversial pesticide.

In May, Clean Water Action, Sierra Club, and other environmental organizations sued the EPA over its indefinite stay of effluent limitation guidelines for power plants.

In June, several health and farmworker associations sued the EPA for delaying a rule that would have imposed stricter standards on who can apply pesticides. The rule would have prohibited young farmworkers from doing so and would have required safety training for others.

In July, Attorney General Eric Schneiderman of New York and 10 other state attorneys general sued the EPA over its delay of a rule meant to protect workers and first responders from accidents at chemical facilities.

In August, the NRDC sued the EPA over a stay on a rule to limit methane emissions from landfills.

At the Texas Environmental Superconference on August 4, NRDC lawyer John Walke said to “expect more lawsuits” against the EPA. The EPA does not comment on pending litigation.

It’s not just deregulation that is coming under legal scrutiny. Laws like the Clean Air Act compel the EPA to regulate pollution, and some environmental advocates are prepared to go to court should the agency fail to act.

The state of Maryland and the Environmental Defense Fund, for example, have notified the EPA of their intent to sue Pruitt over the agency’s nonresponse to the state’s petition for assistance in reducing the pollution that’s blowing across its borders from neighboring states. The state of Connecticut filed a similar lawsuit in May. In these cases, pollution reduction requires federal action because the environmental regulations of downwind states lack jurisdiction in the upwind states.

The courts can push back if Pruitt attempts to undermine climate science altogether

One of the biggest legal obstacles Pruitt may face is repealing or replacing Obama’s signature climate policy, the Clean Power Plan. To do so would require a formal justification. And calling into question the basic science behind climate change likely wouldn’t hold water in court.

Should Pruitt try to formalize his “red-team” idea, in which he would assemble an official panel of climate skeptics to review the prevailing science on climate change, he would likely have to abide by the requirements of the Federal Advisory Committee Act, a law put in place in 1972 to ensure agencies’ use of nonpartisan, objective experts.

Further, any attempt to contest the so-called “endangerment finding,” the Supreme Court-mandated scientific basis for the EPA’s requirement to regulate greenhouse gas emissions, would certainly be challenged in court. Some conservative organizations and industry leaders like the Heritage Foundation and coal magnate Bob Murray want Pruitt to revoke the finding, while other trade groups and fossil fuel interests have reportedly discouraged such an effort.

But arguments against the dangers of global warming and the role of carbon emissions, if made, would likely fail. “If they try [to reverse the “endangerment finding”], they will lose and lose badly,” Walke said in Texas last week.

As Harvard law professor Jody Freeman told me by email, “Courts don't tend to believe in ‘alternative facts.’ Even conservative judges stick closely to the record and can be expected to look skeptically at a misrepresentation of the science.”

While the Trump administration is making some progress in dismantling the EPA — through its hiring freeze, enforcement drop-off, regulatory slowdown — the rule of law (along with vigilance by the public, state attorneys general, and environment and public health organizations that are prepared to mount legal battles) can keep Trump’s and Pruitt’s environmental agenda somewhat in check.