Skip to content

Commit 0ff4b06

Browse files
committed
feat(cli/delete): add delete by urn file option
1 parent 8350a4e commit 0ff4b06

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed

metadata-ingestion/src/datahub/cli/delete_cli.py

+33-21
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,9 @@ def undo_by_filter(
265265
type=str,
266266
help="Urn of the entity to delete, for single entity deletion",
267267
)
268+
@click.option(
269+
"--urn-file", required=True, help="Absolute path of file with urns to be deleted"
270+
)
268271
@click.option(
269272
"-a",
270273
"--aspect",
@@ -353,6 +356,7 @@ def undo_by_filter(
353356
@telemetry.with_telemetry()
354357
def by_filter(
355358
urn: Optional[str],
359+
urn_file: Optional[str],
356360
aspect: Optional[str],
357361
force: bool,
358362
soft: bool,
@@ -373,6 +377,7 @@ def by_filter(
373377
# Validate the cli arguments.
374378
_validate_user_urn_and_filters(
375379
urn=urn,
380+
urn_file=urn_file,
376381
entity_type=entity_type,
377382
platform=platform,
378383
env=env,
@@ -407,11 +412,27 @@ def by_filter(
407412
logger.info(f"Using {graph}")
408413

409414
# Determine which urns to delete.
410-
delete_by_urn = bool(urn) and not recursive
411415
if urn:
412416
urns = [urn]
413-
414-
if recursive:
417+
elif urn_file:
418+
with open(urn_file, "r") as r:
419+
urns = []
420+
for line in r.readlines():
421+
urn = line.strip().strip('"')
422+
urns.append(urn)
423+
else:
424+
urns = list(
425+
graph.get_urns_by_filter(
426+
entity_types=[entity_type] if entity_type else None,
427+
platform=platform,
428+
env=env,
429+
query=query,
430+
status=soft_delete_filter,
431+
batch_size=batch_size,
432+
)
433+
)
434+
if recursive:
435+
for urn in urns:
415436
# Add children urns to the list.
416437
if guess_entity_type(urn) == "dataPlatformInstance":
417438
urns.extend(
@@ -429,25 +450,15 @@ def by_filter(
429450
batch_size=batch_size,
430451
)
431452
)
432-
else:
433-
urns = list(
434-
graph.get_urns_by_filter(
435-
entity_types=[entity_type] if entity_type else None,
436-
platform=platform,
437-
env=env,
438-
query=query,
439-
status=soft_delete_filter,
440-
batch_size=batch_size,
441-
)
453+
if len(urns) == 0:
454+
click.echo(
455+
"Found no urns to delete. Maybe you want to change your filters to be something different?"
442456
)
443-
if len(urns) == 0:
444-
click.echo(
445-
"Found no urns to delete. Maybe you want to change your filters to be something different?"
446-
)
447-
return
457+
return
448458

459+
delete_by_urn = len(urns) == 1
449460
# Print out a summary of the urns to be deleted and confirm with the user.
450-
if not delete_by_urn:
461+
if delete_by_urn:
451462
urns_by_type: Dict[str, List[str]] = {}
452463
for urn in urns:
453464
entity_type = guess_entity_type(urn)
@@ -537,6 +548,7 @@ def process_urn(urn):
537548

538549
def _validate_user_urn_and_filters(
539550
urn: Optional[str],
551+
urn_file: Optional[str],
540552
entity_type: Optional[str],
541553
platform: Optional[str],
542554
env: Optional[str],
@@ -549,9 +561,9 @@ def _validate_user_urn_and_filters(
549561
raise click.UsageError(
550562
"You cannot provide both an urn and a filter rule (entity-type / platform / env / query)."
551563
)
552-
elif not urn and not (entity_type or platform or env or query):
564+
elif not urn and not urn_file and not (entity_type or platform or env or query):
553565
raise click.UsageError(
554-
"You must provide either an urn or at least one filter (entity-type / platform / env / query) in order to delete entities."
566+
"You must provide either an urn or urn file path or at least one filter (entity-type / platform / env / query) in order to delete entities."
555567
)
556568
elif query:
557569
logger.warning(

0 commit comments

Comments
 (0)