Hi,

I will try explain my problem by steps, I think that is more easy
Step 1 - I have upload system to add KML layers to map, after the upload is  complete, execute the kml function.
<a href="http://osgeo-org.1803224.n2.nabble.com/file/n7072383/step1.png" target="_top">step1.png</a>
So far so good.

Step 2 -When I upload a new kml (after first) layer,  the layer is loaded correctly on the map but the first kml  layer continues in map
<a href="http://osgeo-org.1803224.n2.nabble.com/file/n7072383/step2.png" target="_top">step2.png</a>

How can I do to when I upload a new kml the function remove / destroy the previous ?
---------------
My kml function
<pre>
function kml(){
var kml_uri = 'https://www.fileplace.org.pt/websig/tmp/';
        var kml_file_uri = document.getElementById('kml_file').value;
        var MyKML = new OpenLayers.Layer.GML(&quot;KML&quot;, kml_uri+kml_file_uri, 
               {                                                
                format: OpenLayers.Format.KML, 
                formatOptions: {
                  extractStyles: true, 
                  extractAttributes: true,
                  maxDepth: 2
                },
                
               });
               MyKML.events.register(&quot;loadend&quot;,MyKML,function (e) {
    map.zoomToExtent(MyKML.getDataExtent());
        
});

        map.addLayers([MyKML]);
        

 map.setCenter(new OpenLayers.LonLat(-7,39).transform(
        new OpenLayers.Projection(&quot;EPSG:4326&quot;),
        map.getProjectionObject()
    ), 5);

select = new OpenLayers.Control.SelectFeature(MyKML);
            
            MyKML.events.on({
                &quot;featureselected&quot;: onFeatureSelect,
                &quot;featureunselected&quot;: onFeatureUnselect
            });

            map.addControl(select);
            select.activate();   



        
        function onPopupClose(evt) {
            select.unselectAll();
        }
        function onFeatureSelect(event) {
            var feature = event.feature;
            // Since KML is user-generated, do naive protection against
            // Javascript.
            var content = &quot;&lt;h2&gt;&quot;+feature.attributes.name + &quot;&lt;/h2&gt;&quot; + feature.attributes.description;
            if (content.search(&quot;&lt;script&quot;) != -1) {
                content = &quot;Content contained Javascript! Escaped content below.&lt;br&gt;&quot; + content.replace(/&lt;/g, &quot;&amp;lt;&quot;);
            }
            popup = new OpenLayers.Popup.FramedCloud(&quot;chicken&quot;, 
                                      feature.geometry.getBounds().getCenterLonLat(event.xy),
                                     new OpenLayers.Size(100,100),
                                     content,
                                     null, true, onPopupClose);
            feature.popup = popup;
            map.addPopup(popup);
        }
        function onFeatureUnselect(event) {
            var feature = event.feature;
            if(feature.popup) {
                map.removePopup(feature.popup);
                feature.popup.destroy();
                delete feature.popup;
            }
        }
                
}
</pre>
        
<br/><hr align="left" width="300" />
View this message in context: <a href="http://osgeo-org.1803224.n2.nabble.com/remove-destroy-previous-kml-layers-tp7072383p7072383.html">remove / destroy previous kml layers</a><br/>
Sent from the <a href="http://osgeo-org.1803224.n2.nabble.com/OpenLayers-Users-f1822463.html">OpenLayers Users mailing list archive</a> at Nabble.com.<br/>