Python APIPython

How to Remove Orphaned Workspace Sidebar Items in Frappe

Remove orphaned Frappe Workspace Sidebar Items using frappe.get_all and frappe.delete_doc to fix TypeError in the frontend caused by missing workspace links.

Hero image

This snippet demonstrates how to identify and remove orphaned Frappe Workspace Sidebar Items using `frappe.get_all` and `frappe.delete_doc` to resolve frontend TypeErrors.

It identifies and removes `Workspace Sidebar Item` records that reference non-existent `Workspace` documents, resolving related frontend errors.

Use this snippet after a Frappe/ERPNext version update or data migration where orphaned child table entries might cause UI issues.

python
1# Preview first
2existing = set(frappe.get_all("Workspace", pluck="name"))
3bad_items = frappe.get_all(
4 "Workspace Sidebar Item",
5 filters={"link_type": "Workspace"},
6 fields=["name", "parent", "label", "link_to"]
7)
8bad_items = [r for r in bad_items if r.link_to not in existing]
9print(f"Found {len(bad_items)} orphaned items:")
10for row in bad_items:
11 print(row)
12
13# Delete them
14for row in bad_items:
15 frappe.delete_doc("Workspace Sidebar Item", row.name, ignore_permissions=True)
16
17frappe.db.commit()
18print("Done. Deleted", len(bad_items), "orphaned sidebar items.")

Understanding This Code

What It Does

Identifies and removes orphaned Workspace Sidebar Item records that reference non-existent Workspace documents, cleaning up database inconsistencies that cause frontend TypeErrors.

When To Use

Use this snippet after a Frappe/ERPNext version update or data migration where orphaned child table entries might cause UI issues. It's particularly useful when encountering 'TypeError: Cannot read properties of undefined (reading 'public')' errors in the frontend.

Prerequisites

  • •Frappe/ERPNext installed and running.
  • •Bench access or access to Frappe console.
  • •Understanding of Frappe DocTypes and database operations.

Key Concepts

Important ideas to understand in this code

Workspace

A Frappe DocType representing a dashboard or module grouping in the UI.

Learn more

Workspace Sidebar Item

A child DocType linked to Workspace, defining items in its sidebar navigation.

Learn more

frappe.get_all

Retrieves records from a specified DocType, optionally filtered.

Learn more

frappe.delete_doc

Permanently deletes a DocType record from the database.

Learn more

frappe.db.commit

Commits pending database changes to make them permanent.

Learn more

Step-by-Step Tutorial

Follow along to understand how this code works

1

Identify Existing Workspaces

Get a set of all active Workspace names to use for validating linked sidebar items.

python
existing = set(frappe.get_all("Workspace", pluck="name"))
Next Step
2

Find Orphaned Sidebar Items

Query all Workspace Sidebar Items and filter for those whose linked Workspace no longer exists.

python
bad_items = frappe.get_all(
    "Workspace Sidebar Item",
    filters={"link_type": "Workspace"},
    fields=["name", "parent", "label", "link_to"]
)
bad_items = [r for r in bad_items if r.link_to not in existing]
Next Step
3

Delete Orphaned Records

Iterate through the identified orphaned items and delete each one using `frappe.delete_doc`, then commit the database changes.

python
for row in bad_items:
    frappe.delete_doc("Workspace Sidebar Item", row.name, ignore_permissions=True)

frappe.db.commit()
Talk to our Experts

Ready to Scale with Smart Tech? Let's Talk.

Transform your business processes with AI-powered automation and expert implementation.

Trusted by clients in 15+ countries since 2016
Questions buyers ask

Frequently Asked Questions

Find answers to common questions about our services