1+ on PURGE and LAYDEL
Welcome to CADTutor, laurell!
Using erase command wont do you any good. If there are entiteis on layouts other than model or inside a block on the layer to be deleted
_Laydel will delete these entities and the layer itself regardless of space , only thing you need to do is ensure its unlock and not the current layer.
This code will delete all layers except those on the list
(defun c:demo (/ aDoc name)
(setvar 'Clayer "0")
(repeat 4
(vla-purgeall
(setq aDoc (vla-get-ActiveDocument
(vlax-get-acad-object)))))
(vlax-for
itm
(vla-get-layers aDoc)
(if (and
(not (wcmatch
(setq name (vla-get-name itm))
"*|*"))
(not (eq name "0"))
(not (member name
'("thislayer1"
"thislayer2"
"thislayer3"))))
(progn
(vla-put-lock itm :vlax-false)
(vl-cmdf "_.-laydel" "_N" name "" "_Y"))
)
)
(repeat 4 (vla-purgeall aDoc))
(princ)
)
Now if you want to delete only those on the list
replace this line
[b](not [/b](member name '("thislayer1" "thislayer2" "thislayer3"))[b])[/b]
with this
(member name '("thislayer1" "thislayer2" "thislayer3"))
HTH