Cleanup lead owners

From TomGranot/hubspot-admin-skills · View interactive version

Cleanup lead owners is an AI agent skill from the GitHub repository TomGranot/hubspot-admin-skills. Remove non-employee users from HubSpot and reassign their orphaned contacts, companies, and deals. Pairs with the assign-unowned-contacts skill for comprehensive ownership cleanup.

How to use this skill

Skill file

Cleanup Lead Owners

Remove departed employees from HubSpot and reassign their CRM records. Orphaned records with no active owner fall through the cracks.

Prerequisites

Step-by-Step Instructions

Stage 1: Plan

Confirm with the user before starting:

  1. Who receives reassigned records — a single default owner, or territory/round-robin rules?
  2. Which flagged users should be deactivated vs merely stripped of records?

Stage 2: Before

Identify non-employee owners via the Owners API:

import os, requests
from dotenv import load_dotenv

load_dotenv()
TOKEN = os.environ["HUBSPOT_ACCESS_TOKEN"]
BASE = "https://api.hubapi.com"
HEADERS = {"Authorization": f"Bearer {TOKEN}", "Content-Type": "application/json"}

active = requests.get(f"{BASE}/crm/v3/owners", headers=HEADERS,
                      params={"limit": 100, "archived": "false"}).json()["results"]
deactivated = requests.get(f"{BASE}/crm/v3/owners", headers=HEADERS,
                           params={"limit": 100, "archived": "true"}).json()["results"]

Cross-reference with your current employee list. Flag:

For each flagged owner, count how many contacts, companies, and deals they own.

Stage 3: Execute

  1. Reassign records owned by non-employees:
  1. Deactivate users who are no longer employees (requires Super Admin in HubSpot Settings > Users & Teams).
  1. Run /assign-unowned-contacts after reassignment to catch any records that ended up without an owner.

Stage 4: After

  1. Search for contacts where hubspot_owner_id matches any deactivated owner ID — count should be zero.
  2. Confirm all reassigned contacts have an active owner.
  3. Check that no workflows broke due to owner changes (some workflows may filter by specific owners).

Rollback

Tips

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.