What it does
Delete Empty Layers with the DELEMPTY AutoLISP command
Finds layers with no referenced entities, excludes current and protected layers, prints candidates, and requires explicit confirmation before deletion.
- AutoLISP command
- DELEMPTY
- Difficulty
- Advanced
Applicable disciplines
CAD workflows that use this routine
Requirements and compatibility
What it needs
Software rows marked “Not tested” are not compatibility claims. Test the routine in a copy of your drawing and CAD version.
Downloadable source code
Review the delete-empty-layers.lsp AutoLISP code
;; CADLisp Tools - Delete Empty Layers
(vl-load-com)
(defun ct-de:add-layer (name items)
(if (and name (not (member (strcase name) items)))
(cons (strcase name) items)
items))
(defun ct-de:used-layers (/ block data ent i name set used)
(setq used nil)
(if (setq set (ssget "_X"))
(progn
(setq i 0)
(repeat (sslength set)
(setq data (entget (ssname set i))
used (ct-de:add-layer (cdr (assoc 8 data)) used)
i (1+ i)))))
(setq block (tblnext "BLOCK" T))
(while block
(setq ent (tblobjname "BLOCK" (cdr (assoc 2 block))))
(while (and (setq ent (entnext ent))
(setq data (entget ent))
(/= (cdr (assoc 0 data)) "ENDBLK"))
(setq used (ct-de:add-layer (cdr (assoc 8 data)) used)))
(setq block (tblnext "BLOCK")))
used)
(defun c:DELEMPTY (/ *error* answer candidates current deleted doc failed flags layers
name result row undo-started used)
(defun *error* (message)
(if (and undo-started doc)
(vl-catch-all-apply 'vla-EndUndoMark (list doc)))
(if (and message (not (wcmatch (strcase message) "*CANCEL*,*QUIT*,*EXIT*")))
(prompt (strcat "\nDELEMPTY error: " message)))
(princ))
(setq current (strcase (getvar "CLAYER"))
used (ct-de:used-layers)
row (tblnext "LAYER" T)
candidates nil)
(while row
(setq name (cdr (assoc 2 row)) flags (cdr (assoc 70 row)))
(if (and (not (member (strcase name) (list "0" "DEFPOINTS" current)))
(= 0 (logand flags 80))
(not (vl-string-search "|" name))
(not (member (strcase name) used)))
(setq candidates (cons name candidates)))
(setq row (tblnext "LAYER")))
(if candidates
(progn
(prompt "\nCandidate empty layers:")
(foreach name (reverse candidates) (prompt (strcat "\n " name)))
(initget "Yes No")
(setq answer (getkword "\nDelete these layers? [Yes/No] <No>: "))
(if (= answer "Yes")
(progn
(setq doc (vla-get-ActiveDocument (vlax-get-acad-object))
layers (vla-get-Layers doc)
undo-started (not (vl-catch-all-error-p
(vl-catch-all-apply 'vla-StartUndoMark (list doc))))
deleted 0 failed 0)
(foreach name candidates
(setq result
(vl-catch-all-apply 'vla-delete
(list (vla-item layers name))))
(if (vl-catch-all-error-p result)
(progn
(setq failed (1+ failed))
(prompt (strcat "\nCould not delete referenced/protected layer: " name)))
(setq deleted (1+ deleted))))
(if undo-started
(progn (vla-EndUndoMark doc) (setq undo-started nil)))
(prompt (strcat "\nDeleted " (itoa deleted) " layer(s); "
(itoa failed) " retained.")))
(prompt "\nNo layers deleted.")))
(prompt "\nNo deletable empty layers found."))
(princ))
Setup
How to install the delete-empty-layers.lsp file
- 1Download and review the source code.
- 2Load the .lsp file with APPLOAD in a copy of a drawing.
- 3Keep the routine session-only until it has been tested for your CAD version and standards.
Command workflow
How to run the DELEMPTY command
- 1Run DELEMPTY.
- 2Review the candidate layer list.
- 3Type Yes only if the list is safe to remove.
Worked example
Example: Remove unused imported layers.
- Run DELEMPTY.
- Review candidates.
- Confirm deletion.
Deletable unused layers are removed; failures are reported.
Read before use
Notes and limitations
- Layer 0, Defpoints, current, xref-dependent, and referenced layers are retained.
- Deletion is not a substitute for standards review.
- Not manually CAD-tested.
Version history