Function: delete-directory
delete-directory is an interactive and byte-compiled function defined
in files.el.gz.
Signature
(delete-directory DIRECTORY &optional RECURSIVE TRASH)
Documentation
Delete the directory named DIRECTORY. Does not follow symlinks.
If RECURSIVE is non-nil, delete files in DIRECTORY as well, with
no error if something else is simultaneously deleting them.
TRASH non-nil means to trash the directory instead, provided
delete-by-moving-to-trash is non-nil.
When called interactively, TRASH is nil if and only if a prefix argument is given, and a further prompt asks the user for RECURSIVE if DIRECTORY is nonempty.
Other relevant functions are documented in the file group.
Probably introduced at or before Emacs version 23.2.
Key Bindings
Shortdoc
;; file
(delete-directory "/tmp/bar/")
Aliases
org-delete-directory (obsolete since 9.0)
Source Code
;; Defined in /usr/src/emacs/lisp/files.el.gz
(defun delete-directory (directory &optional recursive trash)
"Delete the directory named DIRECTORY. Does not follow symlinks.
If RECURSIVE is non-nil, delete files in DIRECTORY as well, with
no error if something else is simultaneously deleting them.
TRASH non-nil means to trash the directory instead, provided
`delete-by-moving-to-trash' is non-nil.
When called interactively, TRASH is nil if and only if a prefix
argument is given, and a further prompt asks the user for
RECURSIVE if DIRECTORY is nonempty."
(interactive
(let* ((trashing (and delete-by-moving-to-trash
(null current-prefix-arg)))
(dir (expand-file-name
(read-directory-name
(if trashing
"Move directory to trash: "
"Delete directory: ")
default-directory default-directory nil nil))))
(list dir
(if (directory-files dir nil directory-files-no-dot-files-regexp)
(y-or-n-p
(format-message "Directory `%s' is not empty, really %s? "
dir (if trashing "trash" "delete")))
nil)
(null current-prefix-arg))))
;; If default-directory is a remote directory, make sure we find its
;; delete-directory handler.
(setq directory (directory-file-name (expand-file-name directory)))
(let ((handler (find-file-name-handler directory 'delete-directory)))
(cond
(handler
(funcall handler 'delete-directory directory recursive trash))
((and delete-by-moving-to-trash trash)
;; Move non-empty dir to trash only if recursive deletion was
;; requested. This mimics the non-`delete-by-moving-to-trash'
;; case, where the operation fails in delete-directory-internal.
;; As `move-file-to-trash' trashes directories (empty or
;; otherwise) as a unit, we do not need to recurse here.
(if (not (or recursive (directory-empty-p directory)))
(error "Directory is not empty, not moving to trash")
(move-file-to-trash directory)))
;; Otherwise, call ourselves recursively if needed.
(t
(when (or (not recursive) (file-symlink-p directory)
(let* ((files
(files--force t #'directory-files directory 'full
directory-files-no-dot-files-regexp))
(directory-exists (listp files)))
(when directory-exists
(mapc (lambda (file)
;; This test is equivalent to but more efficient
;; than (and (file-directory-p fn)
;; (not (file-symlink-p fn))).
(if (eq t (car (file-attributes file)))
(delete-directory file recursive)
(files--force t #'delete-file file)))
files))
directory-exists))
(files--force recursive #'delete-directory-internal directory))))))