Delete Lakebase Resources
This recipe demonstrates how to programmatically delete Lakebase PostgreSQL resources from your Databricks workspace using FastAPI. This endpoint safely removes all created Lakebase resources to avoid ongoing costs.
This endpoint permanently deletes:
- Database catalog and all its data
- PostgreSQL database instance and all stored data
This action cannot be undone. Ensure you have backed up any important data before deletion.
The pipeline created to sync orders to our postgres instance will need to be deleted manually.
In this example, we set up our API to be called using the DELETE
HTTP method which is the standard choice for removing resources in REST APIs as defined in RFC 7231.
DELETE requests are idempotent - making the same request multiple times has the same effect as making it once.
For detailed specifications, refer to RFC 7231 Section 4.3.5 which defines the DELETE method's semantics and requirements.
Code snippet
import logging
import os
from databricks.sdk import WorkspaceClient
from models.lakebase import LakebaseResourcesDeleteResponse
from fastapi import APIRouter, Query
# Environment variables used:
# LAKEBASE_INSTANCE_NAME=my-lakebase-instance
# LAKEBASE_CATALOG_NAME=my-pg-catalog
logger = logging.getLogger(__name__)
w = WorkspaceClient()
router = APIRouter(tags=["lakebase"])
current_user_id = w.current_user.me().id
@router.delete(
"/resources/delete-lakebase-resources",
response_model=LakebaseResourcesDeleteResponse,
summary="Delete Lakebase Resources",
)
async def delete_lakebase_resources(
confirm_deletion: bool = Query(
description="""🚨 This endpoint will permanently delete Lakebase resources.
Set to true to confirm you want to delete these resources. 🚨
⌛️ This endpoint may take a few minutes to complete.⌛️""",
),
):
if not confirm_deletion:
return LakebaseResourcesDeleteResponse(
deleted_resources=[],
failed_deletions=[],
message="No resources were deleted (confirm_deletion=False)",
)
instance_name = os.getenv("LAKEBASE_INSTANCE_NAME", f"{current_user_id}-lakebase-demo")
catalog_name = os.getenv("LAKEBASE_CATALOG_NAME", f"{current_user_id}-pg-catalog")
synced_table_name = f"{catalog_name}.public.orders_synced"
deleted_resources = []
failed_deletions = []
# Delete synced table
logger.info(f"Attempting to delete synced table: {synced_table_name}")
try:
w.database.delete_synced_database_table(name=synced_table_name)
deleted_resources.append(f"Synced table: {synced_table_name}")
logger.info(f"Successfully deleted synced table: {synced_table_name}")
except Exception as e:
failed_deletions.append(f"Synced table: {synced_table_name} - {str(e)}")
logger.error(f"Failed to delete synced table {synced_table_name}: {e}")
# Delete catalog
logger.info(f"Attempting to delete catalog: {catalog_name}")
try:
w.database.delete_database_catalog(name=catalog_name)
deleted_resources.append(f"Catalog: {catalog_name}")
logger.info(f"Successfully deleted catalog: {catalog_name}")
except Exception as e:
failed_deletions.append(f"Catalog: {catalog_name} - {str(e)}")
logger.error(f"Failed to delete catalog {catalog_name}: {e}")
# Delete database instance
logger.info(f"Attempting to delete database instance: {instance_name}")
try:
w.database.delete_database_instance(name=instance_name, purge=True)
deleted_resources.append(f"Database instance: {instance_name}")
logger.info(f"Successfully deleted database instance: {instance_name}")
except Exception as e:
failed_deletions.append(f"Database instance: {instance_name} - {str(e)}")
logger.error(f"Failed to delete database instance {instance_name}: {e}")
if failed_deletions:
message = f"Deletion completed with errors. {len(deleted_resources)} resources deleted, {len(failed_deletions)} failed."
else:
message = f"All {len(deleted_resources)} resources deleted successfully."
return LakebaseResourcesDeleteResponse(
deleted_resources=deleted_resources,
failed_deletions=failed_deletions,
message=message,
)
The above example is shortened for brevity and not suitable for production use. You can find a more advanced sample in the databricks-apps-cookbook GitHub repository.
Example Usage
# Delete all Lakebase resources
curl -X DELETE "http://localhost:8000/api/v1/resources/delete-lakebase-resources?confirm_deletion=true"
# Safe call without deletion (for testing)
curl -X DELETE "http://localhost:8000/api/v1/resources/delete-lakebase-resources?confirm_deletion=false"
If the request was successful, you will get the following output:
{
"deleted_resources": [
"Synced table: my-pg-catalog.public.orders_synced",
"Catalog: my-pg-catalog",
"Database instance: my-lakebase-instance"
],
"failed_deletions": [],
"message": "All 3 resources deleted successfully."
}
Resources
Permissions
Your app service principal needs the following permissions:
- Workspace admin privileges to delete database instances
DROP
permissions on the Unity Catalog objects- Ownership or admin access to the database instance
- Access to delete synced table pipelines
See Lakebase permissions for more information.
Dependencies
- Databricks SDK for Python -
databricks-sdk
- FastAPI -
fastapi
- uvicorn -
uvicorn
databricks-sdk>=0.60.0
fastapi
uvicorn