CXPURGEClean-upVersion 1.0.0Tested in AutoCAD
Purge the drawing until nothing more goes
Runs purge repeatedly until nothing more can be removed, since unused items often hide inside other unused items, and reports what was removed.
How to use it
- Type CXPURGE.
- Wait for the rounds to finish.
- Read how many blocks, layers, linetypes and styles were removed.
Good to know
- Anything still in use is kept. U undoes the purge.
Load it in AutoCAD
- Save the file anywhere you like.
- Type APPLOAD, pick the file and choose Load.
- Type the command name: CXPURGE.
To have it at every start, add it to the Startup Suite in the same dialog, or load a whole folder at once with the optional loader. About the optional loader
Tested in AutoCAD (full version). Other programs that run AutoLISP may work, but they are not tested.
The code
Plain text, nothing hidden. Copy it into a file named cxpurge.lsp, or download it.
;;; ============================================================================
;;; CXPURGE - Purge the drawing until nothing more goes
;;; Version 1.0.0
;;; CADXChange LISP tools - https://cadxchange.net/resources/lisp/cxpurge
;;; Tested in: AutoCAD (full version). Other programs that run AutoLISP may work but are not tested.
;;; Copyright (c) CADXChange. Free for personal and commercial use; do not resell or
;;; republish it as a collection. Terms: https://cadxchange.net/legal/lisp-terms
;;; ============================================================================
;; How many entries the tables that PURGE touches hold: (blocks layers linetypes styles dimstyles).
(defun cx:purge-counts ( / out r)
(foreach tbl '("BLOCK" "LAYER" "LTYPE" "STYLE" "DIMSTYLE")
(setq r (tblnext tbl T) out (cons 0 out))
(while r
(setq out (cons (1+ (car out)) (cdr out)) r (tblnext tbl))))
(reverse out))
;; Purge unused blocks, layers, linetypes, styles and the like until nothing more goes. Nested definitions need
;; several rounds. Returns the entries removed per table, in the order of cx:purge-counts.
(defun cx:purge-run ( / before after)
(setq before (cx:purge-counts))
(repeat 4
(command "_.-PURGE" "_A" "*" "_N"))
(setq after (cx:purge-counts))
(mapcar '- before after))
(defun c:CXPURGE ( / *error* cx:sv r)
(setq *error* cx:err)
(cx:start '(("CMDECHO" . 0)))
(setq r (cx:purge-run))
(cx:end)
(princ (strcat "\nRemoved: " (itoa (nth 0 r)) " blocks, " (itoa (nth 1 r)) " layers, " (itoa (nth 2 r)) " linetypes, "
(itoa (nth 3 r)) " text styles, " (itoa (nth 4 r)) " dimension styles."))
(princ))
;;; ---- Shared helpers (used by the command above; identical in every CADXChange tool) ----
;; Close the undo step and put the system variables back.
(defun cx:end ()
(foreach v cx:sv (setvar (car v) (cdr v)))
(command "_.UNDO" "_End")
(setq cx:sv nil)
(princ))
;; Error handler: restores everything, stays quiet on Esc.
(defun cx:err (msg)
(if (> (getvar "CMDACTIVE") 0) (command))
(cx:end)
(if (and msg (not (wcmatch (strcase msg) "*BREAK*,*CANCEL*,*EXIT*")))
(princ (strcat "\nError: " msg)))
(princ))
;; Save the system variables in VARS ((name . value) ...), set them, and open one undo step.
;; The caller declares cx:sv as a local so the saved values live exactly as long as the command.
(defun cx:start (vars)
(setq cx:sv (mapcar (function (lambda (v) (cons (car v) (getvar (car v))))) vars))
(command "_.UNDO" "_Begin")
(foreach v vars (setvar (car v) (cdr v)))
(princ))
(princ "\nCXPURGE loaded. Type CXPURGE to start.")
(princ)
Version history
Version 1.0.0 · 27 de outubro de 2026
First release.
More commands: Clean-up
CXBYLAYERSet color, linetype and lineweight back to ByLayerCXZEROLENFind and delete zero-length lines and polylinesCXEMPTYTXTFind and delete empty textCXFONTCHKList text styles whose font file is missingCXFONTSUBReplace one font file by another in all text stylesCXDELTYPEDelete every object of the type you pickCXDIMRESETReset typed-over dimension text to the measured valueCXDUPDELDelete exact duplicate objectsCXOBJCOUNTCount the objects by type