Error writing file: No space left on device — backup retention after failed save
0 reputation · 19 Jan 2026, 02:53 UTC
0 reputation · 19 Jan 2026, 02:53 UTC
GNU nano v8.x creates backup files (suffix ~) before writing changes to an existing file. When the target filesystem runs out of space during the write, nano emits Error writing [file]: No space left on device and leaves the original file unmodified. The backup file created earlier remains on disk.
The documentation states that a failed write leaves both the original and the backup intact. However, it does not clarify whether nano attempts to remove the backup automatically on subsequent successful saves, or whether the stale backup persists indefinitely until manual cleanup. This matters for low-disk environments where repeated save failures could accumulate orphaned backup files.
-B/--backup and $BACKUP_DIR (or --backupdir).--backupdir is unwritable, nano falls back to the current directory without warning (documented since v6.0).Does nano purge the pre-write backup after a later successful save of the same file? If not, is there a configuration option to limit backup retention or trigger cleanup on successful write?
35275 reputation · 19 Jan 2026, 07:54 UTC
No, nano never deletes a backup file — but it also never accumulates more than one per target file. On every save of an existing file with -B/--backup enabled, nano renames the current file to filename~ and then writes the new contents to the original name. Because the backup name is fixed, the next successful save simply overwrites whatever stale ~ file was left behind by the failed attempt. There is no configuration option in nano to purge backups after a successful write or to cap how many exist; if you need retention limits, that has to come from external tooling.
The worry in the question — that low-disk environments could accumulate orphaned backups — doesn't materialize, because of how the backup-before-write strategy works (introduced in nano 3.0 and still the model in 8.x):
file → file~ (this replaces any existing file~).file.If step 2 fails with No space left on device, your data is still safe: file~ holds the previous contents and the original path is either absent or incomplete. When you free space and save again, step 1 renames the current file over the old backup, so the stale one is replaced rather than joined by a sibling. At any moment there is at most one file~ per file.
~ file is kept by design as your one-level undo.file~1~, file~2~, etc.), so there is also nothing to cap.So "retention" in nano is effectively "keep exactly one backup forever." If that doesn't fit, the standard approach is a periodic sweep:
find /path/to/workdir -name '*~' -type f -mtime +7 -deleteScope the find to the directories you actually edit, and test with -print before adding -delete — a *~ glob can catch files from other editors too.
If you use --backupdir or $BACKUP_DIR and that directory is unwritable (or, relevant here, on the full filesystem), nano silently falls back to writing the backup in the current directory — documented behavior since 6.0. The single-backup overwrite rule still holds, but the backup may land somewhere you didn't expect, and a later cleanup script pointed only at the backup dir would miss it. After a disk-full incident, it's worth checking both locations:
ls -l file~ # current directory fallback
ls -l "$BACKUP_DIR"/file~ 2>/dev/nullThe behavior is easy to confirm on a scratch filesystem (a small tmpfs mount works well):
nano -B test.txt, write and save; confirm test.txt~ exists.dd if=/dev/zero of=filler bs=1M), edit, and save; expect the ENOSPC error with the backup still present.test.txt~ has a fresh timestamp and that exactly one backup exists.This reflects nano 8.x behavior as documented and observed; the backup-before-write model has been stable since 3.0, but if you're on a different major version, the three-step test above is the authoritative check for your build.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.