Skip to content

Instantly share code, notes, and snippets.

@bennyistanto
Created February 11, 2025 08:56
Show Gist options
  • Save bennyistanto/f5b9109984e7afd043ac6c84e811e333 to your computer and use it in GitHub Desktop.
Save bennyistanto/f5b9109984e7afd043ac6c84e811e333 to your computer and use it in GitHub Desktop.
Delete EarthEngine Assets

Delete EarthEngine Assets

WARNING:
This code permanently deletes the specified asset folder and all its contents. Please double‑check the asset path before running.

  1. Authentication and Initialization:
    The script begins by calling ee.Authenticate() (only when needed) and ee.Initialize().

  2. delete_folder_contents Function:

    • Uses ee.data.listAssets({'parent': folder}) to list all children assets.
    • Iterates over each child:
      • If the child is a folder (its type is "Folder"), it calls itself recursively to delete its contents, then deletes the folder.
      • Otherwise, it deletes the file asset directly.
    • A short sleep (time.sleep(0.1)) is added to avoid rate limiting.
  3. delete_asset_folder Function:

    • Calls delete_folder_contents() on the target folder.
    • Then attempts to delete the folder itself.
  4. Execution:
    The asset_folder variable is set to our target asset folder. Running delete_asset_folder(asset_folder) deletes all its contents and then the folder itself.

Run this code in our Jupyter Notebook or in Terminal by executing the py file, and it will delete the asset folder and all of its contents in one go without manual confirmation. Use with extreme caution!

import ee
import time
# Authenticate and initialize Earth Engine.
ee.Authenticate() # Run only if needed.
ee.Initialize()
def delete_folder_contents(folder):
"""
Recursively delete all assets contained in the given folder.
Parameters:
folder (str): Full asset path of the folder (e.g.,
'projects/ee-bennyistanto-personal/assets/EDDI/ET0_Climatology_1991_2020').
"""
print("Listing assets in folder:", folder)
try:
assets = ee.data.listAssets({'parent': folder}).get('assets', [])
except Exception as e:
print("Error listing assets for", folder, ":", e)
return
if not assets:
print("No assets found in", folder)
for asset in assets:
asset_id = asset['id']
asset_type = asset.get('type', '')
print("Found", asset_type, "asset:", asset_id)
# If the asset is a folder, delete its contents first recursively.
if asset_type == 'Folder':
delete_folder_contents(asset_id)
# Now delete the folder itself.
try:
ee.data.deleteAsset(asset_id)
print("Deleted folder:", asset_id)
except Exception as e:
print("Error deleting folder", asset_id, ":", e)
else:
# Otherwise, it is a file; delete it.
try:
ee.data.deleteAsset(asset_id)
print("Deleted asset:", asset_id)
except Exception as e:
print("Error deleting asset", asset_id, ":", e)
# Pause briefly to help avoid rate limits.
time.sleep(0.1)
def delete_asset_folder(folder):
"""
Deletes all contents of the folder recursively and then deletes the folder.
Parameters:
folder (str): Full asset path of the folder to delete.
"""
# First, delete all children.
delete_folder_contents(folder)
# Then, delete the parent folder.
try:
ee.data.deleteAsset(folder)
print("Deleted folder:", folder)
except Exception as e:
print("Error deleting folder", folder, ":", e)
# Specify the asset folder you wish to delete.
asset_folder = 'projects/ee-bennyistanto-personal/assets/EDDI/ET0_Climatology_1991_2020'
# Run the recursive deletion.
delete_asset_folder(asset_folder)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment