Skip to main content
Known Participant
May 26, 2025
Answered

Script to delete layers by color or attribute

  • May 26, 2025
  • 2 replies
  • 1903 views

I would like to know if it is possible to delete layers that do not have a lock or color attribute using scripts. Thank you very much in advance.

Correct answer c.pfaffenbichler
quote

That's the bit that is easier said than done...


By @Stephen Marsh


Yes it should be, anyway, thanks for the help


// 2025, use it at your own risk;
if (app.documents.length > 0) {
var theLayers = collectSelectedLayersWithoutColorAndLock ();
for (var m = 0; m < theLayers.length; m++) {
deleteLayerByID(theLayers[m][1], false)
}
};
////// collect bounds of selected layers //////
function collectSelectedLayersWithoutColorAndLock () {
// set to pixels;
var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
// get selected layers;
var selectedLayers = new Array;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var desc = executeActionGet(ref);
if (desc.getBoolean(stringIDToTypeID("hasBackgroundLayer")) == true) {var theAdd =0}
else {var theAdd = 1};
if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){
desc = desc.getList( stringIDToTypeID( 'targetLayers' ));
var c = desc.count;
var selectedLayers = new Array();
// run through selected layers;
for(var i=0;i<c;i++){
var theIndex = desc.getReference( i ).getIndex()+theAdd;
// get id for solid color layers;
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID("Lyr "), theIndex ); 
var layerDesc = executeActionGet(ref);
var checkColor = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("color")));
var checkLock = layerDesc.getObjectValue(stringIDToTypeID("layerLocking"));
var lockAll = checkLock.getBoolean(stringIDToTypeID("protectAll"));
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theIdentifier = layerDesc.getInteger(stringIDToTypeID ("layerID"));
if (checkColor == "none" && lockAll == false) {selectedLayers.push([theName, theIdentifier, ])};
} catch (e) {};
};
// if only one:
}else{
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var layerDesc = executeActionGet(ref);
try {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var checkColor = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("color")));
var checkLock = layerDesc.getObjectValue(stringIDToTypeID("layerLocking"));
var lockAll = checkLock.getBoolean(stringIDToTypeID("protectAll"));
var theIdentifier = layerDesc.getInteger(stringIDToTypeID ("layerID"));
if (checkColor == "none" && lockAll == false) {selectedLayers = [[theName, theIdentifier, ]]}
} catch (e) {};
};
// reset;
app.preferences.rulerUnits = originalRulerUnits;
return selectedLayers;
};
////// by paul riggott //////
function deleteLayerByID(ID) {
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putIdentifier(charIDToTypeID('Lyr '), ID);
desc.putReference( charIDToTypeID('null'), ref );
executeAction( charIDToTypeID('Dlt '), desc, DialogModes.NO );
};

2 replies

Stephen Marsh
Community Expert
Community Expert
May 26, 2025

@Delta2487 

 

Yes, this is possible, however, the devil is in the details...

 

  • What if a layer has a lock but no colour label (or a colour label but no lock)?
  • Which lock type? There are 5 locking options... Is it "Lock All" or also one of the other 4 lock options?
  • What about the special Background layer if present? This is locked and can't have a color label applied.
  • What about layer groups? If a layer group had it's child layers deleted and therefore resulted in an empty group, should the group be removed?

 

As a starting point:

/*
Delete Unlocked Layers Without Color Label.jsx
Stephen Marsh
v1.0 - 27th May 2025
https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-to-delete-layers-by-color-or-attribute/m-p/15341191
*/

// Single history step
app.activeDocument.suspendHistory("Delete Unlocked Layers Without Color Label", "main()");

function main() {
    deleteUnlockedLayersWithoutColorLabel(app.activeDocument.layers);
}

// Helper functions
function getLayerColorLabel(layer) {
    try {
        var ref = new ActionReference();
        ref.putIdentifier(charIDToTypeID("Lyr "), layer.id);
        var desc = executeActionGet(ref);
        var keyColor = stringIDToTypeID("color");
        if (desc.hasKey(keyColor)) {
            var colorType = desc.getEnumerationValue(keyColor);
            var colorName = typeIDToStringID(colorType);
            return colorName !== "none" ? colorName : null;
        }
    } catch (e) {
        // Unable to get color label
    }
    return null; // No color label
}

function deleteUnlockedLayersWithoutColorLabel(layers) {
    for (var i = layers.length - 1; i >= 0; i--) {
        var layer = layers[i];
        if (layer.isBackgroundLayer) {
            // Skip removing the Background layer
            // Comment out if you want to remove the Background layer
            continue;
        }
        if (layer.typename === "ArtLayer") {
            if (!layer.allLocked && getLayerColorLabel(layer) === null) {
                try {
                    layer.remove();
                } catch (e) { }
            }
        } else if (layer.typename === "LayerSet") {
            // First process child layers
            deleteUnlockedLayersWithoutColorLabel(layer.layers);
            // Remove empty groups
            if (layer.layers.length === 0 && !layer.allLocked) {
                // Comment out if desired
                layer.remove();
            }
        }
    }
}

 

  1. Copy the code text to the clipboard
  2. Open a new blank file in a plain-text editor (not in a word processor)
  3. Paste the code in
  4. Save as a plain text format file – .txt
  5. Rename the saved file extension from .txt to .jsx
  6. Install or browse to the .jsx file to run (see below)

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html

Delta2487Author
Known Participant
May 27, 2025

@Stephen Marsh  Thanks for your reply, I want the script to delete only the selected layers that are not locked (lock all)

Stephen Marsh
Community Expert
Community Expert
May 27, 2025
quote

@Stephen Marsh  Thanks for your reply, I want the script to delete only the selected layers that are not locked (lock all)

 

@Delta2487 – I invite you to try the script posted earlier (it doesn't work on selected layers, it loops over all layers). It has a single history step undo, just in case the results are not as expected.

Legend
May 26, 2025

If we can imagine it, it's highly likely that we can accomplish our need! The Adobe scribes, those robed, incense-burning, script-writing coders who lurk here, will be along with your solution, I'm sure, Delta.

 

Larry