Q:

How to remove files from recentf / ivy-virtual-buffers?

I find ivy's virtual buffers feature ((setq ivy-use-virtual-buffers t)) very handy for navigating to files whose buffers have been closed. Like ido's virtual buffers,it uses recentf to present those files as if their buffers were still open when switching buffers.
However, occasionally I want to remove clutter from ivy-switch-buffer by removing 'virtual buffers'. ido has C-k, which does what I want. I've found that ivy's kill action does not always work for this purpose.
How can I reliably remove virtual buffers from the ivy-switch-buffer list, preferably using ivy itself?

A:

Ivy already has an action bound to "k" for ivy-switch-buffer, but it just calls kill-buffer so it does not work on virtual buffers. 
Based on what ido is doing, here's some code to replace the default ivy kill action with one that also handles virtual buffers by dropping them from the recentf list:
(defun my-ivy-kill-buffer (buf)
  (interactive)
  (if (get-buffer buf)
      (kill-buffer buf)
    (setq recentf-list (delete (cdr (assoc buf ivy--virtual-buffers)) recentf-list))))

(ivy-set-actions
 'ivy-switch-buffer
 '(("k"
    (lambda (x)
      (my-ivy-kill-buffer x)
      (ivy--reset-state ivy-last))
    "kill"
    )))

With this configuration you can use the various ivy action dispatch mechanisms depending on what you want to do:

Choose a candidate and hit M-o k to kill it and exit.
Choose a candidate and hit C-M-o k to kill it and then select another candidate.

This seems pretty useful, I may submit it as a pull request to ivy. 

