GNU Emacs
Emacs
Dashboard

30.3 Deleting Files with Dired

One of the most frequent uses of Dired is to first flag files for deletion, then delete the files that were flagged.

d

Flag this file for deletion ( dired-flag-file-deletion).

u

Remove the deletion flag ( dired-unmark).

DEL

Move point to previous line and remove the deletion flag on that line ( dired-unmark-backward).

x

Delete files flagged for deletion ( dired-do-flagged-delete).

You can flag a file for deletion by moving to the line describing the file and typing d ( dired-flag-file-deletion). The 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. u ( 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. However, if the variable dired-recursive-deletes is non- nil, then Dired is allowed to delete nonempty directories including all their contents. That can be somewhat risky. If the value of the variable is always, Dired will delete nonempty directories recursively, which is even more risky.

Even if you have set dired-recursive-deletes to nil, you might want sometimes to delete directories recursively without being asked for confirmation for all of them. For example, you may want that 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 to delete, if you answer all, then all the remaining directories will be deleted without any further questions.

If you change the variable delete-by-moving-to-trash to t, the above deletion commands will move the affected files or directories into the operating system’s Trash, instead of deleting them outright. See Miscellaneous File Operations.

An alternative way of deleting files is to mark them with m and delete with D, see Operating on Files.