The problem is that they did not work except for in some narrow straights.
If you look at the actual landings that did happen during WW2, very few ships was lost due to coastal batteries. The guns where either destroyed or missed their targets.
Some batteries, like in Norway, did deny traffic within their effective fire range in good weather. BUT that was not a complete HOI sea province. And if the allied forces decided on invading Norway or if the area was important for allied shipping, you could rest assure these batteries would have been rendered inoperative.
The allied strategy concerning Norway was to make Hitler believe there where an invasion in the planning, in order to make him waste resources on fortifying Norway with things like coastal guns...
About how easy they where to destroy. The technology of bombing heavy fortified complexes made enormous leaps forward during WW2. In the beginning the gigantic steel reinforced concrete bunkers made for coastal guns and submarines where hard do destroy and required large number of bombers. But the development of things like the bunker busters and air mines changed that dramatically.
I found this quote at U-boat bunker:
Those bunkers where at least as good as the gun bunkers. Also there is no need for complete destruction to make an gun battery inoperative. Even the blast wave itself often killed the crew and dislodged the equipment.
We have a bit different understanding of how they should work actually. I agree that the level of protection was limited and the actual hits were rare. But on the other hand how many succesful landings in ports have you seen during WW2? Or sea-based attack on the docked fleet?
Granted - there were occasions where it succeded (British bombardment of French fleet, raid on Scapa Flow and so on) as well as good examples of landings forced through coastal defence. Like Normandy. But they either required succesful guile or MAJOR concentration of force.
So coastal guns had their failings and their role dimished with the increase of airpower significance (as you rightly point out). But still it was cheaper than assigning fleet cover to all of your coast and I suppose just as effective (carriers excluded).