Function Syntax: PurgeUnusedLayers Version: 1.1 Date: 20.06.2026 6:38:54PM N/A Command name PURGEUNUSEDLAYERS (type PURGEUNUSEDLAYERS at the AutoCAD command line) Description This routine provides a dialog-based tool to scan the drawing for unused layers and then delete them either by selecting specific layers or purging all unused layers at once. Prerequisites • Requires Visual LISP COM support via (vl-load-com). • Requires permission to write a temporary DCL file in the folder provided by TEMPPREFIX. User interaction • The command opens a DCL dialog titled Purge Unused Layers. • The dialog supports: • Toggle: Skip Xref layers (*|*). • Edit box: Never delete these layers (comma-separated list, default 0,DEFPOINTS). • Button: Scan to refresh the unused layer list. • List box: multi-select list of detected unused layers. • Button: Purge Selected. • Button: Purge All. • Button: Close. High-level workflow • Builds the dialog definition in memory using PUL:_DCLLines and writes it to a temp DCL file every run. • Loads the DCL with load_dialog, opens it with new_dialog, and runs it with start_dialog. • Performs an initial scan when the dialog opens, then updates automatically when the user changes options. • Scanning logic checks each layer name for entity usage by running ssget "_X" filtered by layer. • Results are displayed in a list box and a status label shows the count of unused layers. • If the user chooses to purge, the routine attempts deletion using -LAYDEL and verifies success by checking layer existence in the table afterwards. Functionalities • Detects unused layers by scanning the drawing database. • Excludes the current layer (CLAYER) from purge candidates. • Always protects 0 and DEFPOINTS from deletion, even if the user does not list them. • Allows user-defined protected layers via a comma-separated list. • Optionally excludes layers that look like xref layers by pattern *|*. • Supports multi-select purging of only the layers chosen in the dialog. • Supports one-click purging of all currently detected unused layers. • Reports results after purge: number deleted and number failed. Unused-layer detection rules • Builds a full layer list using tblnext "LAYER". • A layer is considered unused if no entities exist on it, determined by: ssget "_X" (list (cons 8 layerName)) returns nil. • A layer is excluded from the candidate list if: • It is in the user-defined skip list. • It is the current layer from CLAYER. • Skip Xref layers is enabled and the name matches *|*. Purge behavior • Purge Selected: • Reads the list_box selection indexes returned as a space-separated string. • Converts indexes to a list using PUL:_ParseIdxList. • Maps indexes back to layer names and attempts to delete only those layers. • Purge All: • Attempts to delete every layer in the current unused-layer list. Important commands and calls highlighted PUL:_EnsureDCL — writes the DCL file to the temp directory each run to avoid stale dialogs. action_tile — wires Scan and option changes to live refresh of results. tblnext — enumerates layers in the drawing. ssget "_X" with layer filter — detects whether any entities exist on a layer. -LAYDEL via vl-cmdf — attempts to delete layers. vl-catch-all-apply — prevents hard failures if -LAYDEL errors on protected or constrained layers. tblsearch "LAYER" — verifies whether a layer was actually removed. Outputs and messages • On load: prints Loaded: PURGEUNUSEDLAYERS (dialog-based unused layer purge). • If cancelled: prints Cancelled. • If Purge Selected with nothing selected: prints No layers selected. • If no unused layers exist: prints No unused layers found. • After purging: prints Deleted: N | Failed: M. Operational notes and limitations • A layer can appear unused by entity scan but still fail deletion due to dependencies, locks, plot states, viewport usage, or other AutoCAD constraints; such cases increment the Failed count. • The unused detection checks only for entities on the layer; it does not explicitly inspect non-entity dependencies such as layer states, filters, dimension style references, or external constraints. • The Skip Xref option relies on name matching *|*, which is a conventional xref layer naming pattern. Description 🧹 PurgeUnusedLayers is an AutoCAD AutoLISP cleanup utility with an embedded DCL dialog for scanning and deleting unused drawing layers. 🔒 It protects key layers such as 0 and DEFPOINTS, can skip Xref layers, and lets users purge selected layers or all detected unused layers. Function Syntax 🚀 Main command: PURGEUNUSEDLAYERS 📌 Load the LSP file with APPLOAD or from your AutoCAD startup suite, then run the command from the command line. SEO Optimized Overview 🧹 This AutoCAD layer purge tool scans the active drawing layer table and identifies layers that have no entities assigned to them. 🎛️ The embedded DCL dialog is written to the temporary folder each run and shows options, status text, and a multi-select list of unused layers. 🔒 The default protected list includes 0 and DEFPOINTS, and the user can add more comma-separated layer names that must never be deleted. 🔗 A Skip Xref layers option protects xref-dependent layers containing the | separator. 🗑️ The user can run Scan, then Purge Selected or Purge All from the dialog. 📊 After deletion, the command reports how many layers were deleted and how many failed to delete. Primary Workflow 1️⃣ Run PURGEUNUSEDLAYERS. 2️⃣ Keep Skip Xref layers enabled when xref layers should be protected. 3️⃣ Review or edit the Never delete layer list, including 0 and DEFPOINTS. 4️⃣ Click Scan to populate the unused-layer list. 5️⃣ Purge only selected layers or purge all listed layers after review. Core Functionalities ✅ Open a DCL dialog for unused-layer cleanup. ✅ Scan all layers in the active drawing for assigned entities. ✅ Protect 0, DEFPOINTS, the current layer, and user-defined skip layers. ✅ Optionally skip Xref-dependent layers matching *|*. ✅ Display unused layers in a multi-select list. ✅ Purge selected unused layers or purge all detected unused layers. ✅ Report deleted and failed layer counts. ✅ Clean up the temporary DCL file after use. Important Commands and Helper Functions 🧩 PUL:_DCLLines returns the embedded DCL layout for the Purge Unused Layers dialog. 🧩 PUL:_EnsureDCL writes purgeunusedlayers.dcl to the AutoCAD temporary folder. 🧩 PUL:_AllLayers reads all layer names from the LAYER table. 🧩 PUL:_ScanUnusedLayers detects unused layers while respecting current layer, protected names, and xref skip rules. 🧩 PUL:_ParseIdxList converts list_box selection indexes into a usable AutoLISP list. 🧩 PUL:_TryLayDel calls -LAYDEL safely for one layer. 🧩 PUL:_PurgeLayers deletes selected layers and counts deleted versus failed results. 🧩 PUL:_UpdateFromDialog refreshes the scan result based on the current dialog options. Result ✅ Unused layers are identified and can be deleted from the active DWG. ✅ Critical layers and xref layers can be protected from accidental removal. ✅ The drawing layer table becomes cleaner for standards checking, plotting, file exchange, and template maintenance. Operational Notes ⚠️ This is a destructive cleanup command because it deletes layers. ⚠️ Use Scan first and review the unused-layer list before choosing Purge All. 📌 Some layers can fail to delete if they are protected, current, referenced by objects not detected by a simple layer selection, or blocked by AutoCAD rules. Best Use Cases 🧹 Cleaning project drawings before issue or archive. 📦 Reducing clutter in layer-heavy Civil 3D files. 🔒 Controlled layer purge with protected names and xref-layer safeguards. Tags 🏷️ AutoCAD, 🏷️ AutoLISP, 🏷️ Civil 3D, 🏷️ Purge Layers, 🏷️ Unused Layers, 🏷️ LAYDEL, 🏷️ Layer Cleanup, 🏷️ DCL Dialog, 🏷️ CAD Standards, 🏷️ Drawing Cleanup