Emacs: Delete Current File

By Xah Lee. Date: . Last updated: .

Here's emacs commands that delete the current file.

Always Make Backup Version

This version will always make a backup file, even when buffer is not a file.

(defun xah-delete-current-file-make-backup (&optional @no-backup-p)
  "Delete current file, makes a backup~, closes the buffer.

Backup filename is “‹name›~‹date time stamp›~”. Existing file of the same name is overwritten. If the file is not associated with buffer, the backup file name starts with “xx_”.

When `universal-argument' is called first, don't create backup.

URL `http://ergoemacs.org/emacs/elisp_delete-current-file.html'
Version 2016-07-20"
  (interactive "P")
  (let* (
         ($fname (buffer-file-name))
         ($buffer-is-file-p $fname)
         ($backup-suffix (concat "~" (format-time-string "%Y%m%dT%H%M%S") "~")))
    (if $buffer-is-file-p
          (save-buffer $fname)
          (when (not @no-backup-p)
             (concat $fname $backup-suffix)
          (delete-file $fname)
          (message "Deleted. Backup created at 「%s」." (concat $fname $backup-suffix)))
      (when (not @no-backup-p)
        (write-region (point-min) (point-max) (concat "xx" $backup-suffix))
        (message "Backup created at 「%s」." (concat "xx" $backup-suffix))))
    (kill-buffer (current-buffer))))

Always Copy to kill-ring Version

This version will always copy buffer content to kill-ring (emacs's clipboard)

(defun xah-delete-current-file-copy-to-kill-ring ()
  "Delete current buffer/file and close the buffer, push content to `kill-ring'.
URL `http://ergoemacs.org/emacs/elisp_delete-current-file.html'
Version 2016-07-20"
    (kill-new (buffer-string))
    (message "Buffer content copied to kill-ring.")
    (when (buffer-file-name)
      (when (file-exists-p (buffer-file-name))
          (delete-file (buffer-file-name))
          (message "Deleted file: 「%s」." (buffer-file-name)))))
    (let ((buffer-offer-save nil))
      (set-buffer-modified-p nil)
      (kill-buffer (current-buffer)))))

Smart Version

This version will create a backup file if the buffer is associated with a file, else copy content to kill-ring (emacs's clipboard).

(defun xah-delete-current-file (&optional @no-backup-p)
  "Delete current buffer/file.
If buffer is a file, makes a backup~, else, push file content to `kill-ring'.

If buffer is dired, go up a dir and mark it for delete  (by `dired-flag-file-deletion').
 (press 【x】 to call `dired-do-flagged-delete'  to actually delete it)

This commands calls `xah-delete-current-file-make-backup' or

If next buffer is dired, refresh it.

URL `http://ergoemacs.org/emacs/elisp_delete-current-file.html'
Version 2017-08-27"
  (interactive "P")
  (if (eq major-mode 'dired-mode)
      (progn (dired-up-directory)
             (dired-flag-file-deletion 1)
      (if (buffer-file-name)
          (xah-delete-current-file-make-backup @no-backup-p)
    (when (eq major-mode 'dired-mode)

These commands are useful if you code dynamic language such as Python and often create temp testing scripts. [see JavaScript in Depth] [see Python: Learn Python in 1 Hour]

My workflow is often to backup current version of script am working on, create a copy in new file, edit code for experiment, run it, and delete it when done.

The following commands are useful for this.

Emacs: Run Current File.

A command to make backup on demand, see Emacs: Backup Current File.

And Emacs: New Empty Buffer.

Also, it's useful when using emacs to view images. [see Emacs: View Image File]

Current File Commands

  1. Emacs: Open File Path Under Cursor
  2. Emacs: Copy File Path of Current Buffer
  3. Emacs: Run Current File
  4. Emacs: Delete Current File
  5. Emacs: Backup Current File
  6. Emacs: Batch Convert Line Ending in Dired
  7. Emacs: Open File in External App
Patreon me $5 patreon

Or Buy Xah Emacs Tutorial

Or buy a nice keyboard: Best Keyboard for Emacs

If you have a question, put $5 at patreon and message me.