Function: delete-file

delete-file is an interactive function defined in fileio.c.

Signature

(delete-file FILENAME &optional TRASH)

Documentation

Delete file named FILENAME. If it is a symlink, remove the symlink.

If file has multiple names, it continues to exist with the other names. TRASH non-nil means to trash the file instead of deleting, provided delete-by-moving-to-trash is non-nil.

When called interactively, TRASH is t if no prefix argument is given. With a prefix argument, TRASH is nil.

Other relevant functions are documented in the file group.

Probably introduced at or before Emacs version 1.6.

Key Bindings

Shortdoc

;; file
(delete-file "/tmp/foo")

Source Code

// Defined in /usr/src/emacs/src/fileio.c
{
  Lisp_Object handler;
  Lisp_Object encoded_file;

  if (!NILP (Ffile_directory_p (filename))
      && NILP (Ffile_symlink_p (filename)))
    xsignal2 (Qfile_error,
	      build_string ("Removing old name: is a directory"),
	      filename);
  filename = Fexpand_file_name (filename, Qnil);

  handler = Ffind_file_name_handler (filename, Qdelete_file);
  if (!NILP (handler))
    return call3 (handler, Qdelete_file, filename, trash);

  if (delete_by_moving_to_trash && !NILP (trash))
    return call1 (Qmove_file_to_trash, filename);

  encoded_file = ENCODE_FILE (filename);

  if (unlink (SSDATA (encoded_file)) != 0 && errno != ENOENT)
    report_file_error ("Removing old name", filename);
  return Qnil;
}