Short 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.

Command:
๐ Main command: PURGEUNUSEDLAYERS
๐งฉ Load the LSP in AutoCAD or Civil 3D, then type the command at the command line.
Description:
๐งน 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.
Helper function: (if any)โ
๐งฉ 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.
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.
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.
Images, animations etc.

XAML code:
๏ปฟ<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:Autodesk.Windows;assembly=AdWindows">
<src:RibbonToolTip x:Key="PurgeUnusedLayers">
<src:RibbonToolTip.ExpandedContent>
<StackPanel>
<TextBlock Background="AntiqueWhite" TextAlignment="Left">
<Bold>Function Syntax: PurgeUnusedLayers</Bold>
<LineBreak/>
<Bold>Version: 1.1 Date: 20.06.2026 6:38:54PM</Bold>
<LineBreak/>
<Hyperlink>N/A</Hyperlink>
<Bold>Command name</Bold><LineBreak/>
<Run>PURGEUNUSEDLAYERS (type </Run><Run Foreground="DarkRed">PURGEUNUSEDLAYERS</Run><Run> at the AutoCAD command line)</Run><LineBreak/>
<Bold>Description</Bold><LineBreak/>
<Run>This routine provides a dialog-based tool to </Run><Run Foreground="DarkRed">scan</Run><Run> the drawing for unused layers and then </Run><Run Foreground="DarkRed">delete</Run><Run> them either by selecting specific layers or purging all unused layers at once.</Run><LineBreak/>
<Bold>Prerequisites</Bold><LineBreak/>
<Run>โข Requires Visual LISP COM support via </Run><Run Foreground="DarkRed">(vl-load-com)</Run><Run>.</Run><LineBreak/>
<Run>โข Requires permission to write a temporary DCL file in the folder provided by </Run><Run Foreground="DarkRed">TEMPPREFIX</Run><Run>.</Run><LineBreak/>
<Bold>User interaction</Bold><LineBreak/>
<Run>โข The command opens a </Run><Run Foreground="DarkRed">DCL dialog</Run><Run> titled Purge Unused Layers.</Run><LineBreak/>
<Run>โข The dialog supports:</Run><LineBreak/>
<Run> โข Toggle: </Run><Run Foreground="DarkRed">Skip Xref layers (*|*)</Run><Run>.</Run><LineBreak/>
<Run> โข Edit box: </Run><Run Foreground="DarkRed">Never delete these layers</Run><Run> (comma-separated list, default 0,DEFPOINTS).</Run><LineBreak/>
<Run> โข Button: </Run><Run Foreground="DarkRed">Scan</Run><Run> to refresh the unused layer list.</Run><LineBreak/>
<Run> โข List box: multi-select list of detected unused layers.</Run><LineBreak/>
<Run> โข Button: </Run><Run Foreground="DarkRed">Purge Selected</Run><Run>.</Run><LineBreak/>
<Run> โข Button: </Run><Run Foreground="DarkRed">Purge All</Run><Run>.</Run><LineBreak/>
<Run> โข Button: </Run><Run Foreground="DarkRed">Close</Run><Run>.</Run><LineBreak/>
<Bold>High-level workflow</Bold><LineBreak/>
<Run>โข Builds the dialog definition in memory using </Run><Run Foreground="DarkRed">PUL:_DCLLines</Run><Run> and writes it to a temp DCL file every run.</Run><LineBreak/>
<Run>โข Loads the DCL with </Run><Run Foreground="DarkRed">load_dialog</Run><Run>, opens it with </Run><Run Foreground="DarkRed">new_dialog</Run><Run>, and runs it with </Run><Run Foreground="DarkRed">start_dialog</Run><Run>.</Run><LineBreak/>
<Run>โข Performs an initial </Run><Run Foreground="DarkRed">scan</Run><Run> when the dialog opens, then updates automatically when the user changes options.</Run><LineBreak/>
<Run>โข Scanning logic checks each layer name for entity usage by running </Run><Run Foreground="DarkRed">ssget "_X"</Run><Run> filtered by layer.</Run><LineBreak/>
<Run>โข Results are displayed in a list box and a status label shows the count of unused layers.</Run><LineBreak/>
<Run>โข If the user chooses to purge, the routine attempts deletion using </Run><Run Foreground="DarkRed">-LAYDEL</Run><Run> and verifies success by checking layer existence in the table afterwards.</Run><LineBreak/>
<Bold>Functionalities</Bold><LineBreak/>
<Run>โข Detects unused layers by scanning the drawing database.</Run><LineBreak/>
<Run>โข Excludes the </Run><Run Foreground="DarkRed">current layer (CLAYER)</Run><Run> from purge candidates.</Run><LineBreak/>
<Run>โข Always protects </Run><Run Foreground="DarkRed">0</Run><Run> and </Run><Run Foreground="DarkRed">DEFPOINTS</Run><Run> from deletion, even if the user does not list them.</Run><LineBreak/>
<Run>โข Allows user-defined protected layers via a comma-separated list.</Run><LineBreak/>
<Run>โข Optionally excludes layers that look like xref layers by pattern </Run><Run Foreground="DarkRed">*|*</Run><Run>.</Run><LineBreak/>
<Run>โข Supports multi-select purging of only the layers chosen in the dialog.</Run><LineBreak/>
<Run>โข Supports one-click purging of all currently detected unused layers.</Run><LineBreak/>
<Run>โข Reports results after purge: number deleted and number failed.</Run><LineBreak/>
<Bold>Unused-layer detection rules</Bold><LineBreak/>
<Run>โข Builds a full layer list using </Run><Run Foreground="DarkRed">tblnext "LAYER"</Run><Run>.</Run><LineBreak/>
<Run>โข A layer is considered unused if no entities exist on it, determined by:</Run><LineBreak/>
<Run> โข <Run Foreground="DarkRed">ssget "_X" (list (cons 8 layerName))</Run> returns nil.</Run><LineBreak/>
<Run>โข A layer is excluded from the candidate list if:</Run><LineBreak/>
<Run> โข It is in the user-defined skip list.</Run><LineBreak/>
<Run> โข It is the current layer from </Run><Run Foreground="DarkRed">CLAYER</Run><Run>.</Run><LineBreak/>
<Run> โข Skip Xref layers is enabled and the name matches </Run><Run Foreground="DarkRed">*|*</Run><Run>.</Run><LineBreak/>
<Bold>Purge behavior</Bold><LineBreak/>
<Run>โข Purge Selected:</Run><LineBreak/>
<Run> โข Reads the list_box selection indexes returned as a space-separated string.</Run><LineBreak/>
<Run> โข Converts indexes to a list using </Run><Run Foreground="DarkRed">PUL:_ParseIdxList</Run><Run>.</Run><LineBreak/>
<Run> โข Maps indexes back to layer names and attempts to delete only those layers.</Run><LineBreak/>
<Run>โข Purge All:</Run><LineBreak/>
<Run> โข Attempts to delete every layer in the current unused-layer list.</Run><LineBreak/>
<Bold>Important commands and calls highlighted</Bold><LineBreak/>
<Run>โข <Run Foreground="DarkRed">PUL:_EnsureDCL</Run> โ writes the DCL file to the temp directory each run to avoid stale dialogs.</Run><LineBreak/>
<Run>โข <Run Foreground="DarkRed">action_tile</Run> โ wires Scan and option changes to live refresh of results.</Run><LineBreak/>
<Run>โข <Run Foreground="DarkRed">tblnext</Run> โ enumerates layers in the drawing.</Run><LineBreak/>
<Run>โข <Run Foreground="DarkRed">ssget "_X"</Run> with layer filter โ detects whether any entities exist on a layer.</Run><LineBreak/>
<Run>โข <Run Foreground="DarkRed">-LAYDEL</Run> via </Run><Run Foreground="DarkRed">vl-cmdf</Run><Run> โ attempts to delete layers.</Run><LineBreak/>
<Run>โข <Run Foreground="DarkRed">vl-catch-all-apply</Run> โ prevents hard failures if -LAYDEL errors on protected or constrained layers.</Run><LineBreak/>
<Run>โข <Run Foreground="DarkRed">tblsearch "LAYER"</Run> โ verifies whether a layer was actually removed.</Run><LineBreak/>
<Bold>Outputs and messages</Bold><LineBreak/>
<Run>โข On load: prints </Run><Run Foreground="DarkRed">Loaded: PURGEUNUSEDLAYERS (dialog-based unused layer purge)</Run><Run>.</Run><LineBreak/>
<Run>โข If cancelled: prints </Run><Run Foreground="DarkRed">Cancelled</Run><Run>.</Run><LineBreak/>
<Run>โข If Purge Selected with nothing selected: prints </Run><Run Foreground="DarkRed">No layers selected</Run><Run>.</Run><LineBreak/>
<Run>โข If no unused layers exist: prints </Run><Run Foreground="DarkRed">No unused layers found</Run><Run>.</Run><LineBreak/>
<Run>โข After purging: prints </Run><Run Foreground="DarkRed">Deleted: N | Failed: M</Run><Run>.</Run><LineBreak/>
<Bold>Operational notes and limitations</Bold><LineBreak/>
<Run>โข 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.</Run><LineBreak/>
<Run>โข 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.</Run><LineBreak/>
<Run>โข The Skip Xref option relies on name matching </Run><Run Foreground="DarkRed">*|*</Run><Run>, which is a conventional xref layer naming pattern.</Run><LineBreak/>
<Bold><Run Foreground="DodgerBlue">Description</Run></Bold><LineBreak/>
<Run Foreground="White">๐งน PurgeUnusedLayers is an AutoCAD AutoLISP cleanup utility with an embedded DCL dialog for scanning and deleting unused drawing layers.</Run><LineBreak/>
<Run Foreground="White">๐ It protects key layers such as 0 and DEFPOINTS, can skip Xref layers, and lets users purge selected layers or all detected unused layers.</Run><LineBreak/>
<LineBreak/>
<Bold><Run Foreground="DodgerBlue">Function Syntax</Run></Bold><LineBreak/>
<Run Foreground="White">๐ Main command: </Run><Bold><Run Foreground="Orange">PURGEUNUSEDLAYERS</Run></Bold><LineBreak/>
<Run Foreground="White">๐ Load the LSP file with APPLOAD or from your AutoCAD startup suite, then run the command from the command line.</Run><LineBreak/>
<LineBreak/>
<Bold><Run Foreground="DodgerBlue">SEO Optimized Overview</Run></Bold><LineBreak/>
<Run Foreground="White">๐งน This AutoCAD layer purge tool scans the active drawing layer table and identifies layers that have no entities assigned to them.</Run><LineBreak/>
<Run Foreground="White">๐๏ธ 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.</Run><LineBreak/>
<Run Foreground="White">๐ The default protected list includes 0 and DEFPOINTS, and the user can add more comma-separated layer names that must never be deleted.</Run><LineBreak/>
<Run Foreground="White">๐ A Skip Xref layers option protects xref-dependent layers containing the | separator.</Run><LineBreak/>
<Run Foreground="White">๐๏ธ The user can run Scan, then Purge Selected or Purge All from the dialog.</Run><LineBreak/>
<Run Foreground="White">๐ After deletion, the command reports how many layers were deleted and how many failed to delete.</Run><LineBreak/>
<LineBreak/>
<Bold><Run Foreground="LimeGreen">Primary Workflow</Run></Bold><LineBreak/>
<Run Foreground="White">1๏ธโฃ Run PURGEUNUSEDLAYERS.</Run><LineBreak/>
<Run Foreground="White">2๏ธโฃ Keep Skip Xref layers enabled when xref layers should be protected.</Run><LineBreak/>
<Run Foreground="White">3๏ธโฃ Review or edit the Never delete layer list, including 0 and DEFPOINTS.</Run><LineBreak/>
<Run Foreground="White">4๏ธโฃ Click Scan to populate the unused-layer list.</Run><LineBreak/>
<Run Foreground="White">5๏ธโฃ Purge only selected layers or purge all listed layers after review.</Run><LineBreak/>
<LineBreak/>
<Bold><Run Foreground="LimeGreen">Core Functionalities</Run></Bold><LineBreak/>
<Run Foreground="White">โ
Open a DCL dialog for unused-layer cleanup.</Run><LineBreak/>
<Run Foreground="White">โ
Scan all layers in the active drawing for assigned entities.</Run><LineBreak/>
<Run Foreground="White">โ
Protect 0, DEFPOINTS, the current layer, and user-defined skip layers.</Run><LineBreak/>
<Run Foreground="White">โ
Optionally skip Xref-dependent layers matching *|*.</Run><LineBreak/>
<Run Foreground="White">โ
Display unused layers in a multi-select list.</Run><LineBreak/>
<Run Foreground="White">โ
Purge selected unused layers or purge all detected unused layers.</Run><LineBreak/>
<Run Foreground="White">โ
Report deleted and failed layer counts.</Run><LineBreak/>
<Run Foreground="White">โ
Clean up the temporary DCL file after use.</Run><LineBreak/>
<LineBreak/>
<Bold><Run Foreground="DodgerBlue">Important Commands and Helper Functions</Run></Bold><LineBreak/>
<Run Foreground="White">๐งฉ PUL:_DCLLines returns the embedded DCL layout for the Purge Unused Layers dialog.</Run><LineBreak/>
<Run Foreground="White">๐งฉ PUL:_EnsureDCL writes purgeunusedlayers.dcl to the AutoCAD temporary folder.</Run><LineBreak/>
<Run Foreground="White">๐งฉ PUL:_AllLayers reads all layer names from the LAYER table.</Run><LineBreak/>
<Run Foreground="White">๐งฉ PUL:_ScanUnusedLayers detects unused layers while respecting current layer, protected names, and xref skip rules.</Run><LineBreak/>
<Run Foreground="White">๐งฉ PUL:_ParseIdxList converts list_box selection indexes into a usable AutoLISP list.</Run><LineBreak/>
<Run Foreground="White">๐งฉ PUL:_TryLayDel calls -LAYDEL safely for one layer.</Run><LineBreak/>
<Run Foreground="White">๐งฉ PUL:_PurgeLayers deletes selected layers and counts deleted versus failed results.</Run><LineBreak/>
<Run Foreground="White">๐งฉ PUL:_UpdateFromDialog refreshes the scan result based on the current dialog options.</Run><LineBreak/>
<LineBreak/>
<Bold><Run Foreground="LimeGreen">Result</Run></Bold><LineBreak/>
<Run Foreground="White">โ
Unused layers are identified and can be deleted from the active DWG.</Run><LineBreak/>
<Run Foreground="White">โ
Critical layers and xref layers can be protected from accidental removal.</Run><LineBreak/>
<Run Foreground="White">โ
The drawing layer table becomes cleaner for standards checking, plotting, file exchange, and template maintenance.</Run><LineBreak/>
<LineBreak/>
<Bold><Run Foreground="DarkOrange">Operational Notes</Run></Bold><LineBreak/>
<Run Foreground="DarkRed">โ ๏ธ This is a destructive cleanup command because it deletes layers.</Run><LineBreak/>
<Run Foreground="DarkRed">โ ๏ธ Use Scan first and review the unused-layer list before choosing Purge All.</Run><LineBreak/>
<Run Foreground="White">๐ 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.</Run><LineBreak/>
<LineBreak/>
<Bold><Run Foreground="DodgerBlue">Best Use Cases</Run></Bold><LineBreak/>
<Run Foreground="White">๐งน Cleaning project drawings before issue or archive.</Run><LineBreak/>
<Run Foreground="White">๐ฆ Reducing clutter in layer-heavy Civil 3D files.</Run><LineBreak/>
<Run Foreground="White">๐ Controlled layer purge with protected names and xref-layer safeguards.</Run><LineBreak/>
<LineBreak/>
<Bold><Run Foreground="Yellow">Tags</Run></Bold><LineBreak/>
<Run Foreground="Yellow">๐ท๏ธ AutoCAD, ๐ท๏ธ AutoLISP, ๐ท๏ธ Civil 3D, ๐ท๏ธ Purge Layers, ๐ท๏ธ Unused Layers, ๐ท๏ธ LAYDEL, ๐ท๏ธ Layer Cleanup, ๐ท๏ธ DCL Dialog, ๐ท๏ธ CAD Standards, ๐ท๏ธ Drawing Cleanup</Run><LineBreak/>
</TextBlock>
<Grid>
<Image Source="Image_000.jpg" Stretch="Uniform"/>
</Grid>
<!-- Use MediaElement for GIF (static first frame) and Image as fallback -->
</StackPanel>
</src:RibbonToolTip.ExpandedContent>
</src:RibbonToolTip>
</ResourceDictionary>
