Skip to content

Commit 137f4d3

Browse files
jx2leekevinjqliu
andauthored
Add --purge option to drop table CLI (#3718)
* add flags in drop table cmd * add drop cmd in cli.md * include purgable with flags * modified cli md and default val in purge option * Apply suggestion --------- Co-authored-by: Kevin Liu <kevinjqliu@users.noreply.github.com>
1 parent de74336 commit 137f4d3

3 files changed

Lines changed: 38 additions & 3 deletions

File tree

‎mkdocs/docs/cli.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,17 @@ Property write.metadata.delete-after-commit.enabled removed from nyc.taxis
240240
➜ pyiceberg properties get table nyc.taxis write.metadata.delete-after-commit.enabled
241241
Could not find property write.metadata.delete-after-commit.enabled on nyc.taxis
242242
```
243+
244+
You can drop a table from the catalog:
245+
246+
```sh
247+
➜ pyiceberg drop table nyc.taxis
248+
Dropped table: nyc.taxis
249+
```
250+
251+
To request purge for the underlying table files through the configured catalog, pass `--purge`:
252+
253+
```sh
254+
➜ pyiceberg drop table nyc.taxis --purge
255+
Dropped table: nyc.taxis (purge requested)
256+
```

‎pyiceberg/cli/console.py‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,14 +269,19 @@ def drop() -> None:
269269

270270
@drop.command()
271271
@click.argument("identifier")
272+
@click.option("--purge", is_flag=True, help="Request that the catalog purge all table files.", default=False)
272273
@click.pass_context
273274
@catch_exception()
274-
def table(ctx: Context, identifier: str) -> None: # noqa: F811
275+
def table(ctx: Context, identifier: str, purge: bool) -> None: # noqa: F811
275276
"""Drop a table."""
276277
catalog, output = _catalog_and_output(ctx)
277278

278-
catalog.drop_table(identifier)
279-
output.text(f"Dropped table: {identifier}")
279+
if purge:
280+
catalog.purge_table(identifier)
281+
else:
282+
catalog.drop_table(identifier)
283+
purge_message = " (purge requested)" if purge else ""
284+
output.text(f"Dropped table: {identifier}{purge_message}")
280285

281286

282287
@drop.command() # type: ignore

‎tests/cli/test_console.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,22 @@ def test_drop_table(catalog: InMemoryCatalog) -> None:
340340
assert result.output == """Dropped table: default.my_table\n"""
341341

342342

343+
def test_drop_table_with_purge(catalog: InMemoryCatalog, mocker: MockFixture) -> None:
344+
catalog.create_namespace(TEST_TABLE_NAMESPACE)
345+
catalog.create_table(
346+
identifier=TEST_TABLE_IDENTIFIER,
347+
schema=TEST_TABLE_SCHEMA,
348+
partition_spec=TEST_TABLE_PARTITION_SPEC,
349+
)
350+
purge_table = mocker.spy(catalog, "purge_table")
351+
352+
runner = CliRunner()
353+
result = runner.invoke(run, ["drop", "table", "default.my_table", "--purge"])
354+
assert result.exit_code == 0
355+
assert result.output == """Dropped table: default.my_table (purge requested)\n"""
356+
purge_table.assert_called_once_with("default.my_table")
357+
358+
343359
def test_drop_table_does_not_exists(catalog: InMemoryCatalog) -> None:
344360
# pylint: disable=unused-argument
345361

0 commit comments

Comments
 (0)