Skip to content

Commit

Permalink
commands: try to delete invalid assets that can't be fetched anyway.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 700712180
  • Loading branch information
Google Earth Engine Authors committed Nov 27, 2024
1 parent 59ea836 commit ad8c814
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions python/ee/cli/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,12 @@ class RmCommand:
"""Deletes the specified assets."""

name = 'rm'
recursive_types = {
ee.data.ASSET_TYPE_FOLDER,
ee.data.ASSET_TYPE_IMAGE_COLL,
ee.data.ASSET_TYPE_FOLDER_CLOUD,
ee.data.ASSET_TYPE_IMAGE_COLL_CLOUD,
}

def __init__(self, parser: argparse.ArgumentParser):
parser.add_argument(
Expand All @@ -1069,22 +1075,29 @@ def run(
) -> None:
config.ee_init()
for asset in args.asset_id:
self._delete_asset(asset, args.recursive, args.verbose, args.dry_run)
recursive = args.recursive
if recursive:
info = self._safe_get_info(asset, args.verbose)
recursive = info and info['type'] in self.recursive_types
self._delete_asset(asset, recursive, args.verbose, args.dry_run)

def _safe_get_info(self, asset_id, verbose):
try:
return ee.data.getInfo(asset_id)
except ee.EEException as e:
if verbose:
print('Failed to get info for %s. %s' % (asset_id, e))
return None

def _delete_asset(self, asset_id, recursive, verbose, dry_run):
"""Attempts to delete the specified asset or asset collection."""
if recursive:
info = ee.data.getInfo(asset_id)
if info is None:
print('Asset does not exist or is not accessible: %s' % asset_id)
return
if info['type'] in (ee.data.ASSET_TYPE_FOLDER,
ee.data.ASSET_TYPE_IMAGE_COLL,
ee.data.ASSET_TYPE_FOLDER_CLOUD,
ee.data.ASSET_TYPE_IMAGE_COLL_CLOUD):
children = ee.data.getList({'id': asset_id})
for child in children:
self._delete_asset(child['id'], True, verbose, dry_run)
if verbose:
print('Listing children of asset: %s' % asset_id)
children = ee.data.getList({'id': asset_id})
for child in children:
recursive = child['type'] in self.recursive_types
self._delete_asset(child['id'], recursive, verbose, dry_run)
if dry_run:
print('[dry-run] Deleting asset: %s' % asset_id)
else:
Expand Down

0 comments on commit ad8c814

Please sign in to comment.