       (quelpa-save-cache)
       (setq quelpa-p t))
     (package-delete (cadr (assq name package-alist)) force-p)
-    (unless (package-installed-p name)
+    (when (or (not (package-installed-p name))
+              (package-built-in-p name))
       (let ((pkg-build-dir (expand-file-name (symbol-name name) quelpa-build-dir)))
         (when (and quelpa-p (file-directory-p pkg-build-dir))
           (delete-directory pkg-build-dir t)))


(defmacro doom--condition-case! (&rest body)
  `(condition-case-unless-debug ex
       (condition-case ex2
           (progn ,@body)
         ('file-error
          (print! (bold (red "  FILE ERROR: %s" (error-message-string ex2))))
          (print! "  Trying again...")
          (quiet! (doom-refresh-packages-maybe t))
          ,@body))
     ('user-error
      (print! (bold (red "  ERROR: %s" ex))))
     ('error
      (doom--refresh-pkg-cache)
      (print! (bold (red "  FATAL ERROR: %s" ex))))))

(defun doom--refresh-pkg-cache ()
  "Clear the cache for `doom-refresh-packages-maybe'."

;;;###autoload
(defun doom-delete-package (name &optional force-p)
  "Uninstalls package NAME if it exists, and clears it from `quelpa-cache'."
  (cl-check-type name symbol)
  (doom-initialize-packages)
  (unless (package-installed-p name)
    (user-error "%s isn't installed" name))
  (let ((inhibit-message (not doom-debug-mode))
