Sometimes Vim crashes and leaves me a swapfile. That's awesome. Then I open the file I was editing and Vim asks me if I want to recover. I do, thanks. When it's done, Vim tells me,

You may want to delete the .swp file now.

Why, yes, I do. How do I do that? I figured it would just start using the old swapfile as a swapfile again and clean it up when I quit, but that's not true. It makes a new one, cleans that one up, and when I open the file again it prompts me again to recover from the first one.

Surely I'm missing something.

  • 1
    See here vi.stackexchange.com/questions/13518/… "The "Delete it" option isn't displayed if the Vim process is still running" and note the comment re TMUX sessions i.e. be sure you don't have Vim still running (also, GNU screen) in the background somewhere, see here vi.stackexchange.com/questions/13518/… – zenaan Oct 1 '19 at 0:50
  • this can happen, for instance, if you did <C-z> because some silly mapping used this but the mapping was commented out / removed. In which case, you should do fg from terminal to get back that vim session. – TamaMcGlinn Jan 28 at 11:17

14 Answers 14


A slightly simpler way:

From the terminal emulator:

vim filename

Then choose recover, and if you want to delete the swap you write (save) and then do this from within vim:

:e          // no argument needed. It assumes the current buffer.

...and choose delete this time.

I just found out about this, and I think I'll do that from now on. Then I won't accidentally overwrite a working file either, if the recovery turned out corrupt (though I've never tried that before).

There are also relevant tips on this question on Stack Overflow (where I found this tip).

  • 25
    I don't a delete option, there is only: [O]pen (E)dit (R)ecover (Q)uit (A)bort, what should I do? – Arne Oct 31 '15 at 17:24
  • 6
    @Arne: From my experience that usually means the file is currently opened in another instance of vim? – miyalys Oct 31 '15 at 17:32
  • 3
    The [D]elete option only appeared after I updated to Vim 7.4 – jmlarson Mar 30 '17 at 15:09
  • @Arne I had the same issue. But after some time, a hour or so, I restarted vim and the option was available. PC wasn't restarted in that time. vim may think that another vim process is running. That's why it doesn't show the option. – ka3ak Apr 24 '17 at 11:02
  • 13
    For those who say they don't see a (D)elete option, see :help swap-exists; quote On Unix this choice is only offered when the process that created the swap file does not appear to be running. I.e. perhaps you have another vim process running which is causing this option to be hidden. It might also explain why you have a swp file in the first place ! – Lqueryvg Apr 1 '18 at 12:34

To Clean out ALL vim Swap Files in a Directory:

If you are sure you don’t need any vim swap files in a directory tree and want to get rid of them, you can use the following command in the directory while vim is not running (not even in another window or even a different login session):

find . -type f -name "*.sw[klmnop]" -delete

Some inspiration and thoughts came from Removing junk .swp files at Google Groups.  This will delete all files whose names end with .swk, .swl, .swm, .swn, .swo, or .swp in the current directory tree.

This might not be what you want, for several reasons:

  • As stated, it searches the current directory tree ; i.e., the current directory and all subdirectories, recursively.  That goes beyond what the question asks for, and may be considered to be overkill.
  • As stated, it will delete all files whose names end with .swk, .swl, .swm, .swn, .swo, or .swp, and not just .swpGary Johnson says,

    Also, not all swap files end in .swp.  If Vim needs to create a swap file and one ending in .swp already exists, Vim will use the extension .swo for the new one and .swn after that.  I think it just continues backwards through the alphabet.  So using something like


    or even


    would be more thorough.

    The original author of this post says,

    'klmnop' may be overkill, but that usually ensures I get all of them.

  • On the other hand, it might be underkill.  rouble suggests

    find . -type f \( -name ".*.s[a-v][a-z]" -o -name ".*.sw[a-p]" \) -delete

    which matches all (lower-case) three-letter extensions ranging from .saa to .swp.  He adds, “Be careful though, while this is a more complete solution, it will take out any .svg image files and .swf adobe flash files.  You may want to modify the regex if you work with those files.”  It also matches .sav, .snd (sound data), .sql, .src, .srt (SubRip video subtitle format), and many other common extensions.

  • It might be overkill.  As stated, it will delete all files whose names end with .swk, .swl, .swm, .swn, .swo, or .swp.  But, apparently, vim swap files are commonly “dot” files.  If you edit hello.cpp, the swap file might be .hello.cpp.swp.  As shown (but not explained) in rouble’s answer, it is safer to delete only files whose names begin with a dot; e.g., with

    find . -type f -name ".*.sw[klmnop]" -delete

Type ls -a which lists ALL the files in the directory

type rm .whatever.your.swp is and press enter

Its that simple.

Any file that shows with . in front is a hidden file and is not normally seen.

Remember that changes are immediate and permanent so be careful.

  • 4
    That's not very simple, though. I have to leave Vim, navigate the swap directory, remember the name of the file, and possibly decide whether it's file.swp or file.swo. Then I have to get back to where I was. Hitting :e is a much simpler solution. – Peeja Dec 21 '13 at 19:41
  • 1
    ls: cannot access +A: No such file or directory – Jonathan Jun 17 '15 at 15:15
  • this is what i need – devugur Nov 3 '18 at 7:12
  • This is probably what most people coming here are looking to do. – GWR Feb 24 at 15:07

Btw. some plugins do that automatically for you: autoswap.vim

from the description:

  1. Is file already open in another Vim session in some other window?
  2. If so, swap to the window where we are editing that file.
  3. Otherwise, if swapfile is older than file itself, just get rid of it.
  4. Otherwise, open file read-only so we can have a look at it and may save it.
  • 8
    Can anyone tell me why vim cannot have sensible behavior by default, without plugins? – Pavel Šimerda Aug 13 '17 at 6:46

No, AFAIK you're missing nothing. Vim continues to keep the swapfile as a backup until you explicitly delete it.

Simply save and quit and reopen the same file again. You'll be prompted (again) with

[O]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort, (D)elete it

Now just press d :)

  • 11
    Really? I mean, I knew that. But that's stupid. It's certainly not "simple". – Peeja Sep 27 '12 at 15:12
  • 1
    By the way, what's the difference between quit and abort? – trusktr Oct 3 '13 at 5:10
  • 6
    there is no Delete option in my case. what should I do? (I saved, quit, and reopened) – Paschalis Dec 4 '14 at 10:14
  • You don't need to quit vim first, just type :e<return> once you have chosen "edit anyway" or "recover" on the file, and you will get the option to delete it – 00prometheus Jul 17 '17 at 23:30

Vim, like most Unix commands, is simple and explicit, rather than assuming and implicit.

You recover with (r). It you don't like the recovery or the recovery is corrupt, you can discard it. If you do like it, you can save the file. The recovery file is not deleted for you because this is not explicit and will not be correct for 100% of situations.

So you (r)ecover, (w)rite and (q)uit, then either edit again and choose (d)elete or "rm .myfile.js.swp" and edit again. This is quick to do and is always, from Vim's point of view, 100% correct behaviour.

  • Okay, then is there a way to get vim to give me the path to the swap file? I'm happy to run a second command to delete the swapfile (in fact, I like the explicitness of that), but I'd like a command to delete the swapfile wherever it is. For instance, I've got a separate swapfile directory. If two open files have the same basename, I can't programmatically decide which is the correct swapfile for the current buffer. – Peeja Sep 27 '13 at 14:33
  • 1
    True, but providing a simple "Recover and delete" option would be nice. It's also probably easy tom implement. I'll do it someday when I'm done going to school if it hasn't been done already. – trusktr Oct 3 '13 at 5:09
  • 2
    Most unix commands are simple, explicit and have sensible default behavior. Vim recovery is not simple, the UX is as bad as in LibreOffice, the behavior is plain wrong and the result, in many cases, is data loss. – Pavel Šimerda Aug 13 '17 at 6:49

Here is a snippet to add in .vimrc. It deletes all the swap files that are associated to the current file buffer and reset swap extension.

function! DeleteFileSwaps()
    let l:output = ''
    redir => l:output 
    silent exec ':sw' 
    redir END 
    let l:current_swap_file = substitute(l:output, '\n', '', '')
    let l:base = substitute(l:current_swap_file, '\v\.\w+$', '', '')
    let l:swap_files = split(glob(l:base.'\.s*'))
    " delete all except the current swap file
    for l:swap_file in l:swap_files
        if !empty(glob(l:swap_file)) && l:swap_file != l:current_swap_file 
            call delete(l:swap_file)
            echo "swap file removed: ".l:swap_file
    " Reset swap file extension to `.swp`.
    set swf! | set swf!
    echo "Reset swap file extension for file: ".expand('%')
command! DeleteFileSwaps :call DeleteFileSwaps()

Once encounter with the predicament, one can execute :DeleteFileSwaps This is great if combine with :windo or :tabdo commands.

:tabdo DeleteFileSwaps 

Further details: A file can have more than 1 swap file. The reason because the swap file, with extension of .swp, still exist and vim will keep creating new ones because of it. To find out if .swp exist:

  • With the target file open in vim, execute :sw to get current swap file.
  • Check the directory that the current swap file is contained in.
  • Then check if directory contains a swap file with the name of the open file and has an extension of .swp.

The snippet above follows the same process, but remove all swap files.

Hope this helps.


These files were annoying for me too, but i set option in .vimrc - set noswapfile to prevent vim create it and, instead, keeping files in memory.

  • According to the OP, swapfiles are useful, so turning them off might not be the best option. – Kusalananda Jun 24 '16 at 12:14
  • Don't turn off swap files. If your machine crashes, you'll lose your work. – Lqueryvg Apr 1 '18 at 11:47
  • Of course this option implies risks, and as i know these swap files were made at the emerging of vi* editiors when it's crashes happened very often. Now i could say vim works stable and they are exists as legacy thing, imho. – Dmitrii Apr 1 '18 at 19:18

Here is a more complete regex to clean all vim swap files based on Github's own gitignore file for Vim

$ find ./ -type f \( -name "\.*\.s[a-v][a-z]" -o -name "\.*\.sw[a-p]" \) -delete

Be careful though, while this is a more complete solution, it will take out any .svg image files and .swf adobe flash files. You may want to modify the regex if you work with those files.


From the manual :h swap:

You can see the name of the current swap file being used with the command: :sw[apname]

  • Warning... that probably isn't name of the file you need to delete. Until you've delete the .swp file, the :sw command will probably give you the name of the .swo file - which is not what you want to delete. – Lqueryvg Apr 1 '18 at 11:54

if you're asked then press D to delete current file's swap file.

if you are not then you have to do it manually.

one alternative way to do this is:

to locate/search file:

find | grep ".searchRefineVertTabs.jsp.swp"

and then delete this file:

rm ./.searchRefineVertTabs.jsp.swp
  • 1
    I am asked, but my choice is to recover. The question is how to delete it just after I've recovered, since it's no longer necessary. It seems like a failure of vim not to do that for me. But the accepted answer is the cleanest workflow I've found so far. – Peeja Sep 24 '15 at 16:00

I often don't see the (D) Delete it option. I guess because I'm on *nix and the .swp file is left over from some crashed or killed vim process. So, if I just want to delete all .sw* files after opening my file I'll issue:

vim my-file-in-current-directory.txt

then hit (E) Edit anyway or (R) Recover

and then once in vim issue:

:!rm .%.sw*

If you're not in the same directory as the file you can still do this, but it's not so easy to remember or type out:

vim my/file/with-really-long-name/in-a-directory-far-away.txt

then (E) or (R) then

:!rm %:p:h/.%:t.sw*
  1. vi filename

  2. recover

  3. :w

  4. :e

  5. delete


You can use,

rm -f .<filename>.<ext>.swp


rm -f .rc.local.swp
  • This technically works, but it's nowhere near as easy at the current accepted answer. – Peeja Mar 18 '15 at 14:36
  • Plus <ext> is redundant here as a file name at least on Linux includes zero or more extensions. – Pavel Šimerda Aug 13 '17 at 6:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.