Skip to content

Commit 6a0c944

Browse files
Set rm_files to be a synchronous method (#503)
1 parent f76842b commit 6a0c944

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Unreleased
55

66
* Respect `AzureBlobFileSystem.protocol` tuple when removing protocols from fully-qualified
77
paths provided to `AzureBlobFileSystem` methods.
8-
8+
* Added `AzureBlobFileSystem.rm_file()`
99

1010
2025.8.0
1111
--------

adlfs/spec.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,27 @@ async def _rm_files(
12871287

12881288
sync_wrapper(_rm_files)
12891289

1290+
async def _rm_file(self, path: str, **kwargs):
1291+
"""Delete a file.
1292+
1293+
Parameters
1294+
----------
1295+
path: str
1296+
File to delete.
1297+
"""
1298+
container_name, p, version_id = self.split_path(path)
1299+
try:
1300+
async with self.service_client.get_container_client(
1301+
container=container_name
1302+
) as cc:
1303+
await cc.delete_blob(p, version_id=version_id)
1304+
except ResourceNotFoundError as e:
1305+
raise FileNotFoundError(
1306+
errno.ENOENT, os.strerror(errno.ENOENT), path
1307+
) from e
1308+
self.invalidate_cache(path)
1309+
self.invalidate_cache(self._parent(path))
1310+
12901311
async def _separate_directory_markers_for_non_empty_directories(
12911312
self, file_paths: typing.Iterable[str]
12921313
) -> typing.Tuple[typing.List[str], typing.List[str]]:

adlfs/tests/test_spec.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2210,3 +2210,48 @@ def test_write_max_concurrency(storage, max_concurrency, blob_size, blocksize):
22102210
with fs.open(path, "rb") as f:
22112211
assert f.read() == data
22122212
fs.rm(container_name, recursive=True)
2213+
2214+
2215+
def test_rm_file(storage):
2216+
fs = AzureBlobFileSystem(
2217+
account_name=storage.account_name,
2218+
connection_string=CONN_STR,
2219+
)
2220+
path = "data/test_file.txt"
2221+
with fs.open(path, "wb") as f:
2222+
f.write(b"test content")
2223+
2224+
assert fs.exists(path)
2225+
fs.rm_file(path)
2226+
with pytest.raises(FileNotFoundError):
2227+
fs.ls(path)
2228+
assert not fs.exists(path)
2229+
assert path not in fs.dircache
2230+
2231+
2232+
def test_rm_file_versioned_blob(storage, mocker):
2233+
from azure.storage.blob.aio import ContainerClient
2234+
2235+
fs = AzureBlobFileSystem(
2236+
account_name=storage.account_name,
2237+
connection_string=CONN_STR,
2238+
version_aware=True,
2239+
)
2240+
mock_delete_blob = mocker.patch.object(
2241+
ContainerClient, "delete_blob", return_value=None
2242+
)
2243+
path = f"data/test_file.txt?versionid={DEFAULT_VERSION_ID}"
2244+
fs.rm_file(path)
2245+
mock_delete_blob.assert_called_once_with(
2246+
"test_file.txt", version_id=DEFAULT_VERSION_ID
2247+
)
2248+
2249+
2250+
def test_rm_file_does_not_exist(storage):
2251+
fs = AzureBlobFileSystem(
2252+
account_name=storage.account_name,
2253+
connection_string=CONN_STR,
2254+
)
2255+
path = "data/non_existent_file.txt"
2256+
with pytest.raises(FileNotFoundError):
2257+
fs.rm_file(path)

0 commit comments

Comments
 (0)