I modified another script into something that works for me, perhaps it will work for you too:
/*
Delete level based on first name
https://community.adobe.com/t5/photoshop/delete-level-based-on-first-name/td-p/12062329
*/
#target photoshop
function main() {
// Call the function to process layers/layerSets
processLayersAndSets(app.activeDocument);
function processLayersAndSets(target) {
// Loop through all layers
for (var l = target.artLayers.length - 1; 0 <= l; l--) {
// Call the removeLayer function
removeLayer(target.artLayers[l]);
}
// Loop through all layer groups/sets
for (var s = target.layerSets.length - 1; 0 <= s; s--) {
// Call the removeLayer function
processLayersAndSets(target.layerSets[s]);
}
// Remove non-background layers beginning with the word "test", case insensitive
function removeLayer(layer) {
if (!layer.isBackgroundLayer & layer.name.match(/^test\b/i) !== null) layer.remove();
}
}
}
app.activeDocument.suspendHistory("Run script", "main()");