Skip to main content

How to Delete a File in Python with Path.unlink

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

Programs make files they later need to get rid of: a draft, a temporary export, a database to rebuild from scratch. Path.unlink() deletes one, and its missing_ok option makes "delete it if it's there" a single line. This guide creates a file, deletes it and deletes it again, with real output from Python 3.15.2.

Syntax​

Syntax
from pathlib import Path

Path(<file path>).unlink()
Path(<file path>).unlink(missing_ok=True)
PartWhat it means
Path(<file path>)The file to delete
.unlink()Deletes it. Raises FileNotFoundError if it isn't there
missing_ok=TrueA file that's already gone isn't an error

Python's pathlib documentation: "Remove this file or symbolic link. If the path points to a directory, use Path.rmdir() instead. If missing_ok is false (the default), FileNotFoundError is raised if the path does not exist."

Prerequisites​

  • uv and Python. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

delete_file.py
from pathlib import Path

draft = Path("draft.txt")
draft.write_text("first draft", encoding="utf-8")
print("Exists:", draft.exists())

draft.unlink()
print("Exists after unlink:", draft.exists())

draft.unlink(missing_ok=True)
print("The second unlink, with missing_ok=True, raised nothing.")

The delete_file.py script in a macOS-style code window: it writes draft.txt, checks it exists, deletes it with unlink, checks again, then calls unlink a second time with missing_ok set to True.

  • write_text() creates draft.txt, so there's a file to delete.
  • The first unlink() deletes it.
  • The second unlink() targets a file that no longer exists, with missing_ok=True.

Step 2: Run It​

Terminal
uv run python delete_file.py
Output
Exists: True
Exists after unlink: False
The second unlink, with missing_ok=True, raised nothing.

The output in a terminal: Exists: True, Exists after unlink: False, and The second unlink, with missing_ok=True, raised nothing.

The file existed, unlink() removed it, and the second call found nothing to delete and carried on quietly. That's the "delete it if it's there" pattern, with no exists() check needed.

Common Mistakes​

MistakeWhat happensDo this instead
unlink() on a file that isn't there, without missing_okFileNotFoundError: [Errno 2] No such file or directory: 'nope.txt'Pass missing_ok=True when a missing file is fine
unlink() on a folderOn macOS: PermissionError: [Errno 1] Operation not permitted: 'adir'Use Path.rmdir() for an empty folder

Troubleshooting​

SymptomLikely causeFix
FileNotFoundError from unlink()The path is wrong, or the file was already deletedCheck the path, or pass missing_ok=True

FAQ​

No. It deletes the file for good, so double-check the path before you call it.

Since when does missing_ok exist?​

Python 3.8. The documentation: "Changed in version 3.8: The missing_ok parameter was added."

Conclusion​

draft.unlink() deleted the file, and draft.unlink(missing_ok=True) then did nothing, without an error, because the file was already gone.

Additional Resources​