Function: dired-delete-file

dired-delete-file is a byte-compiled function defined in dired.el.gz.

Signature

(dired-delete-file FILE &optional RECURSIVE TRASH)

Documentation

Delete FILE or directory (possibly recursively if optional RECURSIVE is true.) RECURSIVE determines what to do with a non-empty directory. The effect of its possible values is:

  nil -- do not delete.
  always -- delete recursively without asking.
  top -- ask for each directory at top level.
  Anything else -- ask for each sub-directory.

TRASH non-nil means to trash the file instead of deleting, provided delete-by-moving-to-trash (which see) is non-nil.

Source Code

;; Defined in /usr/src/emacs/lisp/dired.el.gz
;; Delete file, possibly delete a directory and all its files.
;; This function is useful outside of dired.  One could change its name
;; to e.g. recursive-delete-file and put it somewhere else.
(defun dired-delete-file (file &optional recursive trash) "\
Delete FILE or directory (possibly recursively if optional RECURSIVE is true.)
RECURSIVE determines what to do with a non-empty directory.  The effect of
its possible values is:

  nil           -- do not delete.
  `always'      -- delete recursively without asking.
  `top'         -- ask for each directory at top level.
  Anything else -- ask for each sub-directory.

TRASH non-nil means to trash the file instead of deleting, provided
`delete-by-moving-to-trash' (which see) is non-nil."
       ;; This test is equivalent to
       ;; (and (file-directory-p fn) (not (file-symlink-p fn)))
       ;; but more efficient
       (if (not (eq t (car (file-attributes file))))
           (delete-file file trash)
         (let* ((empty-dir-p (null (directory-files
                                    file t
                                    directory-files-no-dot-files-regexp))))
           (if (and recursive (not empty-dir-p))
               (unless (eq recursive 'always)
                 (let ((prompt
                        (format "Recursively %s %s? "
				(if (and trash delete-by-moving-to-trash)
				    "trash"
				  "delete")
				(dired-make-relative file))))
                   (pcase (read-answer
                           prompt
                           '(("yes"  ?y "delete recursively the current directory")
                             ("no"   ?n "skip to next")
                             ("all"  ?! "delete all remaining directories with no more questions")
                             ("quit" ?q "exit")))
                     ("all" (setq recursive 'always dired-recursive-deletes recursive))
                     ("yes" (if (eq recursive 'top) (setq recursive 'always)))
                     ("no" (setq recursive nil))
                     ("quit" (keyboard-quit))
                     (_ (keyboard-quit))))) ; catch all unknown answers
             (setq recursive nil)) ; Empty dir or recursive is nil.
           (delete-directory file recursive trash))))