Is there a function or recommended best practice to unload layers from the map?

To delete a layer with CMaps Analytics JS API you can define the layer index for which you would like to remove. The following example code illustrates a function called deleteLayer2 which removes the first layer in the map. Then we have a button that triggers the function.

 
 function deleteLayer2(){
 
 cMap.removeLayer(1);
 }
 </script>

</head>

<body onload="buildMap();">

 <div>
 <button id="btnDelLyr" onclick="deleteLayer2()">Delete Layer 2</button>
 <div id="mapDiv" style="position:relative;height:500px;width:500px;"></div>
 </div>