JSFiddle - React, Tailwind, and code Playground

HTML

<div id="firstDiv">
    This is some text
    <span id="firstSpan">Remove this text<span>Keep this text</span></span>
    <span id="secondSpan">Remove<span>Remove<span>Keep!</span>dasfdsfsd</span></span>
</div>

JavaScript

/* Commented it out, i used this to work out how to strip out the text in your example,
   Then i made it recursive!

var newhtml = "";
$('#firstDiv').children().each(function(){

    newhtml += $('<div/>').append(this).html();
    
    
});

$('#firstDiv').html(newhtml);*/

cleartext($('#firstDiv'));

function cleartext(node) {
    
    var children = $(node).children();
    if(children.length > 0) {
    
        var newhtml = "";
        children.each(function() {
            
            cleartext($(this));
           
            newhtml += $('<div/>').append(this).html();
            
        });
        
        $(node).html(newhtml);
        
    }
}