One of the most frequent uses of Dired is to first flag files for deletion, then delete the files that were flagged.
You can flag a file for deletion by moving to the line describing
the file and typing d (
deletion flag is visible as a ‘D’ at the beginning of the line.
This command moves point to the next line, so that repeated d
commands flag successive files. A numeric prefix argument serves as a
repeat count; a negative count means to flag preceding files.
If the region is active, the d command flags all files in the region for deletion; in this case, the command does not move point, and ignores any prefix argument.
The reason for flagging files for deletion, rather than deleting
files immediately, is to reduce the danger of deleting a file
accidentally. Until you direct Dired to delete the flagged files, you
can remove deletion flags using the commands u and <DEL>.
dired-unmark) works just like d, but removes
flags rather than making flags. <DEL>
dired-unmark-backward) moves upward, removing flags; it is
like u with argument −1. A numeric prefix argument to
either command serves as a repeat count, with a negative count meaning
to unflag in the opposite direction. If the region is active, these
commands instead unflag all files in the region, without moving point.
To delete flagged files, type x
dired-do-flagged-delete). This command displays a list of all
the file names flagged for deletion, and requests confirmation with
yes. If you confirm, Dired deletes the flagged files, then
deletes their lines from the text of the Dired buffer. The Dired
buffer, with somewhat fewer lines, remains selected.
If you answer no or quit with C-g when asked to confirm, you return immediately to Dired, with the deletion flags still present in the buffer, and no files actually deleted.
You can delete empty directories just like other files, but normally
Dired cannot delete directories that are nonempty. If the variable
dired-recursive-deletes is non-
nil, then Dired can
delete nonempty directories including all their contents. That can be
somewhat risky. If the value of the variable is
will delete nonempty directories recursively, which is even more
risky. Even if you have set
nil, you might want sometimes to delete recursively directories
without being asked for confirmation for all of them. This is handy
when you have marked many directories for deletion and you are very
sure that all of them can safely be deleted. For every nonempty
directory you are asked for confirmation; if you answer
then all the remaining directories will be deleted without more
If you change the variable
t, the above deletion commands will move the affected files or
directories into the operating system's Trash, instead of deleting
them outright. See Misc File Ops.
An alternative way of deleting files is to mark them with m and delete with D, see Operating on Files.