Restoring deleted dashboards in Looker can sometimes feel like trying to find a needle in a haystack. With the Looker UI only displaying 30 dashboards at a time in the trash folder, imagine me a few weeks ago, desperately scrolling through the trash folder, trying to find dashboard number 2383. It was like a badly designed scavenger hunt, brought by an early morning request via Slack. As I scrolled senselessly, I spotted another message sliding into my peripheral vision. The subject read:
‘Please recover “Policy Actuarial Analysis by Claims Year Dashboard” dashboard’
Another one? After a quick check on the ‘last deleted’ field, it became apparent that nothing had been removed beyond the four-month mark. Now I am tasked to find two dashboards among 6K, scrolling at 30 dashboards at a time. Absolutely not! Looker SDK is here to help.
Here’s the complete Python script, that allows users to restore deleted dashboards using only the Dashboard ID.
import looker_sdk
from looker_sdk import models
# Initialize the SDK
sdk = looker_sdk.init40("looker.ini")
def restore_dashboard_from_trash(dashboard_id: str) -> models.Dashboard:
try:
# Find the dashboard by ID
dashboard = sdk.dashboard(dashboard_id)
# Check if the dashboard is in the trash
if dashboard.deleted:
# Restore the dashboard
sdk.update_dashboard(dashboard.id, models.WriteDashboard(deleted=False))
print(f"Dashboard {dashboard.id} restored.")
return dashboard
else:
print(f"Dashboard with ID {dashboard_id} is not in trash.")
except Exception:
print(f"Dashboard with ID {dashboard_id} not found.")
return None
# Run restore_dashboard_from_trash
dashboard_id = "2383"
restored_dashboard = restore_dashboard_from_trash(dashboard_id)
Let me break down the restore_dashboard_from_trash
function step by step:
- Finding the dashboard by ID: I first start by calling the
sdk.dashboard(dashboard_id)
method to retrieve the dashboard object based on thedashboard_id
. If the dashboard exists, it will be returned as aDashboard
object; otherwise, an exception is raised.
dashboard = sdk.dashboard(dashboard_id)
2. Checking if the dashboard is in the trash: If the dashboard object is retrieved successfully, I check if its deleted
attribute is set to True
. This indicates that the dashboard is in the trash. I did try to search dashboards in the trash by using dashboard_id but this method returned results much more quickly.
if dashboard.deleted:
3. Restoring the dashboard: If the dashboard is in the trash, I call the sdk.update_dashboard()
method with the dashboard.id
and a WriteDashboard
object that has its deleted
attribute set to False
. This restores the dashboard back to its last folder.
sdk.update_dashboard(dashboard.id, models.WriteDashboard(deleted=False))
4. Handling exceptions and different scenarios: I used the good old try-except
block to handle exceptions and different scenarios:
a. If the dashboard is not in the trash, print a message indicating that it’s not deleted.
else:
print(f"Dashboard with ID {dashboard_id} is not in trash.")
b. If the dashboard ID does not exist in Looker, an exception is raised. Catch the exception using a general Exception
class and print a message notifying the user that the dashboard was not found.
except Exception:
print(f"Dashboard with ID {dashboard_id} not found.")
5. Return value: If the dashboard is restored successfully, return the Dashboard
object; otherwise, return None
.
return None
Run restore_dashboard_from_trash to restore dashboard 2383.
dashboard_id = "2383"
restored_dashboard = restore_dashboard_from_trash(dashboard_id)
Hope you find this Python function makes restoring deleted dashboards in Looker a breeze. By using the Looker SDK, you can automate many of the mundane tasks saving hours of work.