Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
9dcb580
feat: delete orphaned files
jayceslesar Apr 29, 2025
e43505c
simpler and a test
jayceslesar Apr 29, 2025
eed5ea8
remove
jayceslesar Apr 29, 2025
8cca600
updates from review!
jayceslesar May 2, 2025
75b1240
include dry run and older than
jayceslesar May 2, 2025
6379480
add case for dry run
jayceslesar May 2, 2025
0c2822e
use .path so we get paths pack
jayceslesar May 3, 2025
aaf8fc2
actually pass in iterable
jayceslesar May 3, 2025
b09641b
capture manifest_list files
jayceslesar May 3, 2025
beec233
refactor into `all_known_files`
jayceslesar May 3, 2025
b888c56
fix type in docstring
jayceslesar May 3, 2025
ff461ed
mildly more readable
jayceslesar May 3, 2025
3b3b10e
beef up tests
jayceslesar May 3, 2025
a62c8cf
make `older_than` required
jayceslesar May 4, 2025
07cbf1b
move under `optimize` namespace
jayceslesar May 4, 2025
54e1e00
add some better logging about what was/was not deleted
jayceslesar May 4, 2025
7c780d3
Merge branch 'main' into feat/orphan-files
jayceslesar May 10, 2025
9b6c9ed
Merge branch 'main' into feat/orphan-files
jayceslesar May 13, 2025
34d10b9
rename optimize -> maintenance
jayceslesar May 17, 2025
0335957
make orphaned_files private
jayceslesar May 17, 2025
9f8145c
correctly coerce list
jayceslesar May 17, 2025
fbdcbd3
add metadata files
jayceslesar May 28, 2025
85b4ab3
Merge branch 'main' into feat/orphan-files
jayceslesar May 28, 2025
c414df8
Merge branch 'main' into feat/orphan-files
jayceslesar Jun 10, 2025
aa9d536
Merge branch 'main' into feat/orphan-files
jayceslesar Jun 21, 2025
b4c14fc
fix test
jayceslesar Jun 21, 2025
f4d98d2
allow older_than to be None
jayceslesar Jun 21, 2025
acd8ed6
Merge branch 'main' into feat/orphan-files
jayceslesar Jul 13, 2025
2a9c607
add partition statistics
jayceslesar Jul 13, 2025
aae92bc
safer
jayceslesar Jul 13, 2025
756e199
Merge branch 'main' into feat/orphan-files
jayceslesar Aug 5, 2025
ad5387a
work with both file IO's
jayceslesar Aug 5, 2025
654a51e
Merge branch 'main' into feat/orphan-files
jayceslesar Sep 22, 2025
f97611a
Update pyiceberg/table/inspect.py
jayceslesar Sep 22, 2025
12f6d44
undo
jayceslesar Sep 22, 2025
2223460
helper dataclass
jayceslesar Sep 22, 2025
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
45 changes: 44 additions & 1 deletion pyiceberg/table/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from __future__ import annotations

import itertools
import logging
import os
import uuid
import warnings
Expand All @@ -31,6 +32,7 @@
Callable,
Dict,
Iterable,
Iterator,
List,
Optional,
Set,
Expand Down Expand Up @@ -62,7 +64,7 @@
inclusive_projection,
manifest_evaluator,
)
from pyiceberg.io import FileIO, load_file_io
from pyiceberg.io import FileIO, _parse_location, load_file_io
from pyiceberg.manifest import (
POSITIONAL_DELETE_SCHEMA,
DataFile,
Expand Down Expand Up @@ -150,6 +152,8 @@

from pyiceberg.catalog import Catalog

logger = logging.getLogger(__name__)

ALWAYS_TRUE = AlwaysTrue()
DOWNCAST_NS_TIMESTAMP_TO_US_ON_WRITE = "downcast-ns-timestamp-to-us-on-write"

Expand Down Expand Up @@ -1371,6 +1375,45 @@ def to_polars(self) -> pl.LazyFrame:

return pl.scan_iceberg(self)

def delete_orphaned_files(self) -> None:
Comment thread
Fokko marked this conversation as resolved.
Outdated
"""Delete orphaned files in the table."""
try:
import pyarrow as pa # noqa: F401
except ModuleNotFoundError as e:
raise ModuleNotFoundError("For deleting orphaned files PyArrow needs to be installed") from e

from pyarrow.fs import FileSelector, FileType

from pyiceberg.io.pyarrow import _fs_from_file_path

location = self.location()
Comment thread
Fokko marked this conversation as resolved.
Outdated

all_known_files = []
Comment thread
Fokko marked this conversation as resolved.
Outdated
snapshots = self.snapshots()
snapshot_ids = [snapshot.snapshot_id for snapshot in snapshots]
all_manifests_table = self.inspect.all_manifests(snapshots)
all_known_files.extend(all_manifests_table["path"].to_pylist())
Comment thread
jayceslesar marked this conversation as resolved.
Outdated

executor = ExecutorFactory.get_or_create()
files_by_snapshots: Iterator["pa.Table"] = executor.map(lambda snapshot_id: self.inspect.files(snapshot_id), snapshot_ids)
all_known_files.extend(pa.concat_tables(files_by_snapshots)["file_path"].to_pylist())
Comment thread
jayceslesar marked this conversation as resolved.
Outdated
Comment thread
Fokko marked this conversation as resolved.
Outdated

fs = _fs_from_file_path(self.io, location)

_, _, path = _parse_location(location)
selector = FileSelector(path, recursive=True)
# filter to just files as it may return directories
all_files = [f.path for f in fs.get_file_info(selector) if f.type == FileType.File]

orphaned_files = set(all_files).difference(set(all_known_files))
logger.info(f"Found {len(orphaned_files)} orphaned files at {location}!")

if orphaned_files:
deletes = executor.map(self.io.delete, orphaned_files)
Comment thread
Fokko marked this conversation as resolved.
Outdated
# exhaust
list(deletes)
logger.info(f"Deleted {len(orphaned_files)} orphaned files at {location}!")

Comment thread
Fokko marked this conversation as resolved.

class StaticTable(Table):
"""Load a table directly from a metadata file (i.e., without using a catalog)."""
Expand Down
4 changes: 2 additions & 2 deletions pyiceberg/table/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,10 +645,10 @@ def data_files(self, snapshot_id: Optional[int] = None) -> "pa.Table":
def delete_files(self, snapshot_id: Optional[int] = None) -> "pa.Table":
return self._files(snapshot_id, {DataFileContent.POSITION_DELETES, DataFileContent.EQUALITY_DELETES})

def all_manifests(self) -> "pa.Table":
def all_manifests(self, snapshots: Optional[list[Snapshot]] = None) -> "pa.Table":
import pyarrow as pa

snapshots = self.tbl.snapshots()
snapshots = snapshots or self.tbl.snapshots()
if not snapshots:
Comment thread
Fokko marked this conversation as resolved.
return pa.Table.from_pylist([], schema=self._get_all_manifests_schema())

Expand Down
69 changes: 69 additions & 0 deletions tests/table/test_delete_orphans.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from pathlib import Path, PosixPath

import pyarrow as pa
import pytest

from pyiceberg.catalog import Catalog
from pyiceberg.schema import Schema
from pyiceberg.types import IntegerType, NestedField, StringType
from tests.catalog.test_base import InMemoryCatalog


@pytest.fixture
def catalog(tmp_path: PosixPath) -> InMemoryCatalog:
catalog = InMemoryCatalog("test.in_memory.catalog", warehouse=tmp_path.absolute().as_posix())
catalog.create_namespace("default")
return catalog


def test_delete_orphaned_files(catalog: Catalog) -> None:
identifier = "default.test_delete_orphaned_files"

schema = Schema(
NestedField(1, "city", StringType(), required=True),
NestedField(2, "inhabitants", IntegerType(), required=True),
# Mark City as the identifier field, also known as the primary-key
identifier_field_ids=[1],
)

tbl = catalog.create_table(identifier, schema=schema)

arrow_schema = pa.schema(
[
pa.field("city", pa.string(), nullable=False),
pa.field("inhabitants", pa.int32(), nullable=False),
]
)

df = pa.Table.from_pylist(
[
{"city": "Drachten", "inhabitants": 45019},
{"city": "Drachten", "inhabitants": 45019},
],
schema=arrow_schema,
)
tbl.append(df)

orphaned_file = Path(tbl.location()) / "orphan.txt"

orphaned_file.touch()
assert orphaned_file.exists()

tbl.delete_orphaned_files()
assert not orphaned_file.exists()