The normal remedy for building without a permit, if all code issues are satisfied, is to pay for a permit and pay a fine for not getting the permit beforehand.

For the judge to order the structure torn down implies that there are serious safety code violations.