Clear out the buffer before replacing it.
diff --git a/plugins/yapf.vim b/plugins/yapf.vim
index cdbb048..24b3510 100644
--- a/plugins/yapf.vim
+++ b/plugins/yapf.vim
@@ -29,6 +29,7 @@
   let l:formatted_text = system(l:cmd, join(getline(1, '$'), "\n") . "\n")
 
   " Update the buffer.
+  execute '1,' . string(line('$')) . 'delete'
   call setline(1, split(l:formatted_text, "\n"))
 
   " Reset cursor to first line of the formatted range.