AutoLISP: Detach All XREFS

I found this a little while ago and noticed that there is no header – so I don’t know who wrote it. Whoever it was – thank you!!

This little routine quickly detaches all XREFS from a drawing. XREFS are not limited to other drawing files. Pictures and PDFs are also considered XREFS. This routine will detach all of these as well.

Here’s how:

  • DETACHALL <enter>
  • That’s it. the routine will remove all XREFs
(defun C:Detachall (/ *error*

mip:layer-status-restore mip:layer-status-save

delete-xref-img-underlay delete-all-dict



(defun *error* (msg)


(princ msg)


) ;_ end of defun

(defun mip:layer-status-restore ()

(foreach item *PD_LAYER_LST*

(if (not (vlax-erased-p (car item)))


'(lambda ()

(vla-put-lock (car item) (cdr (assoc "lock" (cdr item))))


(car item)

(cdr (assoc "freeze" (cdr item)))

) ;_ end of vla-put-freeze

) ;_ end of lambda

) ;_ end of vl-catch-all-apply

) ;_ end of if

) ;_ end of foreach

(setq *PD_LAYER_LST* nil)

) ;_ end of defun

(defun mip:layer-status-save ()

(setq *PD_LAYER_LST* nil)

(vlax-for item (vla-get-layers

(vla-get-activedocument (vlax-get-acad-object))

) ;_ end of vla-get-layers

(setq *PD_LAYER_LST*

(cons (list item

(cons "freeze" (vla-get-freeze item))

(cons "lock" (vla-get-lock item))

) ;_ end of cons


) ;_ end of cons

) ;_ end of setq

(vla-put-lock item :vlax-false)

(if (= (vla-get-freeze item) :vlax-true)


'(lambda () (vla-put-freeze item :vlax-false))

) ;_ end of vl-catch-all-apply

) ;_ end of if

) ;_ end of vlax-for

) ;_ end of defun

(defun delete-xref-img-underlay (/ count txt)


(vlax-for Blk (vla-get-Blocks

(vla-get-activedocument (vlax-get-acad-object))

) ;_ end of vla-get-Blocks

(if (and (= (vla-get-IsXref Blk) :vlax-false)

(not (wcmatch (vla-get-name Blk) "*|*"))

) ;_ end of and


(setq count 0

txt (strcat " Erase Xref and Underlay in "

(vla-get-name Blk)

) ;_ end of strcat

) ;_ end of setq

(grtext -1 txt)

(vlax-for Obj Blk

(setq count (1+ count))

(if (zerop (rem count 10))

(grtext -1 (strcat txt " : " (itoa count)))

) ;_ end of if


(and (vlax-write-enabled-p Obj)


(and ;_ XREF

(= (vla-get-ObjectName obj) "AcDbBlockReference")

(vlax-property-available-p Obj "Path")

) ;_ end of and

(and ;_ UNDERLAY

(wcmatch (vla-get-ObjectName obj) "*Reference")

(vlax-property-available-p Obj "UnderlayName")

) ;_ end of and

(= (vla-get-ObjectName obj) "AcDbRasterImage") ;_ IMAGE

) ;_ end of or

) ;_ end of and

(VL-CATCH-ALL-APPLY 'vla-Delete (list Obj))

) ;_ end of if

) ;_ end of vlax-for

) ;_ end of progn

) ;_ end of if

) ;_ end of vlax-for


) ;_ end of defun

(defun delete-all-dict (dict)

;;; dict - dict name (like "ACAD_IMAGE_DICT", "ACAD_PDFDEFINITIONS" ... )


'(lambda ()




(vla-get-activedocument (vlax-get-acad-object))

) ;_ end of vla-get-dictionaries


) ;_ end of vla-Item


) ;_ end of vlax-map-Collection

) ;_ end of lambda

) ;_ end of vl-catch-all-apply

) ;_ end of defun



(command "_-xref" "_d" "*")

(while (> (getvar "CMDACTIVE") 0) (command))

(mapcar 'delete-all-dict





) ;_ end of list

) ;_ end of mapcar

(command "_.regenall")

(command "_.externalreferences")


) ;_ end of defun

Open the External Reference Dialog/palette by entering XREF or XR at the command line.

This Picture shows the XREFs in the drawing. There is a JPEG (picture), PDF, & drawing file.

In the picture below:
To detach an xref manually – Select the XREF from the list > right click >  select “Detach”

Note: Selecting an xref from the list will highlight that XREF in the drawing area

In the Picture below:
This is how the XREF dialog/Palette looks after running the DETACHALL.lsp or by manually detaching the XREFs from the drawing.



About AutoCAD Tips

This blog serves as a knowledge base for myself (and anyone else) so that I can reference tips & tricks that I have learned and also refer others to it as well. I hope that this blog helps you learn at least one tip to make your drafting/design experience better.
This entry was posted in AutoLISP, Modifying, XREFs. Bookmark the permalink.

2 Responses to AutoLISP: Detach All XREFS

  1. Pingback: AutoLISP: XREFs: Remove All Pics | AutoCAD Tips

  2. bla bla says:

    wokrs great thank alot for the work guys!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s