Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
- `set_key` and `unset_key` no longer leave a `.tmp_*` file behind on Windows when writing a read-only `.env` fails, and the error raised is the one from the failed write rather than from cleaning up the temporary file by [@MohammedAlkindi] in [#686]
- `load_dotenv`, `dotenv_values`, `get_key`, `set_key`, `unset_key` and the CLI `--file` option now expand a leading `~` to the user's home directory by [@veeceey] in [#615]
- `find_dotenv` and the IPython `%dotenv` magic now expand a leading `~` in the file name by [@theskumar] in [#714]
- `unset_key` and `dotenv unset` no longer rewrite the `.env` file when the key isn't in it, so a symlinked `.env` stays a symlink and its line endings and permissions are left alone by [@PDGGK] in [#734]

## [1.2.4] - 2026-10-01

Expand Down Expand Up @@ -467,6 +468,7 @@ os.PathLike]` instead of just `os.PathLike` (#347 by [@bbc2]).
[#700]: https://github.com/theskumar/python-dotenv/pull/700
[#711]: https://github.com/theskumar/python-dotenv/pull/711
[#714]: https://github.com/theskumar/python-dotenv/pull/714
[#734]: https://github.com/theskumar/python-dotenv/pull/734
[790c5c0]: https://github.com/theskumar/python-dotenv/commit/790c5c02991100aa1bf41ee5330aca75edc51311

<!-- contributors -->
Expand All @@ -477,6 +479,7 @@ os.PathLike]` instead of just `os.PathLike` (#347 by [@bbc2]).
[@MohammedAlkindi]: https://github.com/MohammedAlkindi
[@Nicals]: https://github.com/Nicals
[@Nougat-Waffle]: https://github.com/Nougat-Waffle
[@PDGGK]: https://github.com/PDGGK
[@Qwerty-133]: https://github.com/Qwerty-133
[@ReinerBRO]: https://github.com/ReinerBRO
[@ROTl24]: https://github.com/ROTl24
Expand Down
30 changes: 19 additions & 11 deletions src/dotenv/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,10 @@ def set_key(
return True, key_to_set, value_to_set


class _KeyNotFound(Exception):
pass


def unset_key(
dotenv_path: StrPath,
key_to_unset: str,
Expand All @@ -311,17 +315,21 @@ def unset_key(
return None, key_to_unset

removed = False
with rewrite(dotenv_path, encoding=encoding, follow_symlinks=follow_symlinks) as (
source,
dest,
):
for mapping in with_warn_for_invalid_lines(parse_stream(source)):
if mapping.key == key_to_unset:
removed = True
else:
dest.write(mapping.original.string)

if not removed:
try:
with rewrite(
dotenv_path, encoding=encoding, follow_symlinks=follow_symlinks
) as (source, dest):
for mapping in with_warn_for_invalid_lines(parse_stream(source)):
if mapping.key == key_to_unset:
removed = True
else:
dest.write(mapping.original.string)
if not removed:
# Abandon the rewrite: there is nothing to change, and replacing
# the file anyway would, for instance, turn a symlink into a
# regular file.
raise _KeyNotFound()
except _KeyNotFound:
logger.warning(
"Key %s not removed from %s - key doesn't exist.", key_to_unset, dotenv_path
)
Expand Down
35 changes: 35 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,41 @@ def test_unset_key_symlink_to_existing_file(tmp_path):
assert symlink.read_text() == ""


def test_unset_non_existent_value_leaves_file_untouched(dotenv_path):
dotenv_path.write_bytes(b"a=b\r\nc=d\r\n")
inode_before = dotenv_path.stat().st_ino
logger = logging.getLogger("dotenv.main")

with mock.patch.object(logger, "warning") as mock_warning:
result = dotenv.unset_key(dotenv_path, "x")

assert result == (None, "x")
assert dotenv_path.read_bytes() == b"a=b\r\nc=d\r\n"
assert dotenv_path.stat().st_ino == inode_before
assert list(dotenv_path.parent.iterdir()) == [dotenv_path]
mock_warning.assert_called_once_with(
"Key %s not removed from %s - key doesn't exist.", "x", dotenv_path
)


@pytest.mark.skipif(
sys.platform == "win32", reason="symlinks require elevated privileges on Windows"
)
def test_unset_key_symlink_non_existent_value(tmp_path):
target = tmp_path / "target.env"
target.write_text("a=x\n")
symlink = tmp_path / ".env"
symlink.symlink_to(target)
logger = logging.getLogger("dotenv.main")

with mock.patch.object(logger, "warning"):
result = dotenv.unset_key(symlink, "b")

assert result == (None, "b")
assert symlink.is_symlink()
assert target.read_text() == "a=x\n"


@pytest.mark.skipif(
sys.platform == "win32", reason="symlinks require elevated privileges on Windows"
)
Expand Down
Loading