• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
blog.draftsperson.net

blog.draftsperson.net

the art of technical drawing

blog.draftsperson.net

Delete Layer LISP: Erase and Purge a Layer in AutoCAD

September 18, 2020

Last updated July 28, 2026

Purging a layer only works if the layer is empty, and finding the last few objects sitting on a layer you want rid of can take longer than the rest of the cleanup put together. They are frozen, or off, or in a layout you forgot about, or inside a block.

Delete Layer skips the hunt. Point at any object on the layer you want gone, and it erases every object on that layer and purges the layer definition. There is a second command that does the same for several layers in one pass. Both are by Mark Thomas.

What it does in practice

You do not type a layer name. You pick an object, and the routine reads the layer off that object — which is faster and removes the chance of a typo emptying the wrong layer.

It then collects every object on that layer across the whole drawing, not just the visible ones, and deletes them one at a time before purging the layer. Because the search is database-wide, objects on frozen or switched-off layers are included, and so is anything in a layout. That is the behaviour that makes it useful — those are exactly the objects that were stopping the purge.

It sets layer 0 current before it starts, since you cannot purge the current layer. Note that it does not put your original current layer back afterwards, so you will find yourself on layer 0 when it finishes.

A layer holding exactly one object is not cleared

There is an off-by-one in the code worth knowing about, because the symptom is confusing rather than dramatic.

The routine counts the objects it found, then checks that the count is greater than zero before starting to delete — but the number it checks has already had one subtracted from it, because it is being used as a list position rather than a count. On a layer with one object that adjusted number is zero, the check fails, and nothing is deleted. The purge then does nothing either, because the layer is still in use.

So the routine works on layers with two or more objects and quietly does nothing on a layer with exactly one. If a layer refuses to go, that is why: erase the single object by hand and purge normally. The fix in the code is a single character — the comparison needs to allow zero as well.

Command reference

  • DEL-LAYER — erases everything on one selected layer and purges it.
  • MDEL-LAYER — the same for multiple layers in one run.

Note the hyphens. Both names contain one, which is legal in a command name but easy to leave out when typing.

How to load it

Unzip Del-layer.zip and load del-layer.lsp with APPLOAD. Both commands come from the one file. Given what it does, load it when you need it rather than leaving it in the Startup Suite. Our AutoLISP guide covers loading.

Step-by-step usage

  1. Work on a copy, or at least save first. This deletes objects you cannot see, which makes checking the result harder than usual.
  2. Type DEL-LAYER.
  3. At “Select layer to remove:” click any object on the layer you want gone.
  4. Everything on that layer is erased and the layer is purged. You are left on layer 0.
  5. If the layer is still listed, check whether it held exactly one object — see above.

Objects are removed with a low-level delete rather than the ERASE command, so do not rely on a single U to bring them back. Working on a copy is the sensible protection.

Compatibility notes

The file carries no date. It uses selection sets, entity access and the PURGE command, none of which has changed, so it is a reasonable bet on a current release — written-for rather than tested-on, as always.

Plain AutoLISP with no dialog boxes and no Express Tools calls, so nothing requires the full product; LT users on LT 2024 or later, the release that brought AutoLISP to LT, should be able to run it. See what runs on AutoCAD LT.

Download

Name: Delete Layer
Description: A great routine for when you need to clean a drawing up and
delete layers without complications of still having entities on
the layer that you can’t find.
Type: AutoCAD AutoLISP Routine
Author: Mark Thomas
File Size: 2 Kb
Cost: Free
Worked on: AutoCAD
Download File: Del-layer.zip

Related routines

  • Delete All Text — the same idea aimed at an object type rather than a layer.
  • UPDBLOCK — move block contents onto a sensible layer before you start deleting.
  • LLP Suite — a large set of layer management commands.

References

  • AutoLISP: the complete guide
  • Free LISP routines library
  • AutoLISP on AutoCAD LT

Filed Under: AutoLISP Routines

Primary Sidebar

Subscribe to our Youtube Channel

Post CAD drafting jobs on Freelancer.com. and get some free money to do this.

Categories

  • American Steel Industry (4)
  • AutoCAD Articles & Reference (30)
  • AutoCAD Bugs & Problems (19)
  • AutoCAD Tips (29)
  • AutoCAD Tutorials (54)
  • AutoLISP Routines (328)
  • AutoLISP Tutorials (15)
  • CAD Standards (7)
  • Civil 3D (7)
  • Civil Drafting (4)
  • Dimensions (2)
  • Drafting Funnies (71)
  • Drafting History (9)
  • Drafting Standards (84)
  • Electrical Drafting (2)
  • General IT reference (7)
  • Geometry (15)
  • GIS (2)
  • Hatch Patterns (12)
  • Office Life Hacks (3)
  • PT (1)
  • Revit Tutorials (54)
  • Steel Detailing (14)
  • Structural Drafting (26)
  • Technical Dictionary (61)
  • Useful Website Links (19)

Footer

Free downloads

  • Free AutoLISP routines
  • Free hatch patterns
  • SHX fonts & text styles
  • Numbering LISP collection
  • ISO 3098B font

Reference charts

  • AWS weld symbols chart
  • Steel detailing chart
  • Hole & slot sizes
  • Bolt edge distance
  • ANSI & ARCH paper sizes
  • How to fold an A1 drawing

Useful links

  • CAD blocks (DWG)
  • Revit families
  • structuraldrafter.com
  • cad-corner.com
  • About
  • Privacy policy

Provided for free with no warranty · Log in