DS DevShelfHub Projects · AI tools
Tutorials / CrewAI / Reference / Methods / clear_all_global_hooks()
Method Hooks lifecycle

clear_all_global_hooks(): Reference Guide

By DevShelfHub

Removes every registered crew-scoped hook — essential for test isolation.

See the CrewAI methods catalog, CrewAI introduction, kickoff() reference, and core concepts for surrounding context.

What is clear_all_global_hooks()?

CrewAI registers many hook types — before and after LLM calls, before and after tool calls, plus programmatic register_* helpers — in shared global registries so decorators and runtime plugins can compose without threading state through every constructor. That ergonomics trade-off means tests and REPL sessions can leak hooks into later cases unless you explicitly reset. clear_all_global_hooks() drops every registered callable so the next import or test starts from a clean slate.

Use it symmetrically: clear before registering hooks a test cares about, and clear again in teardown so failures mid-test do not poison the rest of the suite. Autouse fixtures are the usual pattern because developers forget manual cleanup when they skip tests or abort pdb sessions.

Do not call this in production request paths — you would strip observability mid-flight. For hot reload in dev servers, pair clears with re-import logic that re-applies only the hooks owned by the module you refreshed.

Use Cases

  • Test isolation
  • Module reloads

Key Features

  • Wipes all hook categories

When NOT to Use

Production code paths — leave hooks installed.

Notes

Order relative to imports

Some packages register hooks at import time. Clearing before importing CrewAI plugins may be necessary in tests that snapshot hook counts — document the expected sequence in your conftest.

Does not undo decorator metadata on classes

clear_all_global_hooks removes registered callables, but class-level decorator state may still exist until you rebuild the crew object. Re-instantiate crews after aggressive clears.

Parallel pytest

Workers in pytest-xdist still share nothing per process, but autouse fixtures must run in each worker. Avoid global hook registration in module scope without a matching clear.

Contrast with per-crew observability

Structured logging or OpenTelemetry spans should not rely solely on clears — they follow request scopes. Hooks are global; treat clears as test-only hygiene unless you truly rebuild everything.

Code Examples

Pytest fixture

python
@pytest.fixture(autouse=True)
def _hooks():
    clear_all_global_hooks()
    yield
    clear_all_global_hooks()

Explicit reset before registering plugin hooks

python
from crewai.hooks import clear_all_global_hooks, register_before_llm_call_hook

def load_plugin_hooks() -> None:
    clear_all_global_hooks()
    register_before_llm_call_hook(plugin_audit)

Notebook cell guard

python
from crewai.hooks import clear_all_global_hooks

# Re-run this cell before experimenting with hooks
clear_all_global_hooks()

When to Use

Test setup, hot-reload setups.

Common Mistakes

❌ Forgetting it and seeing leaked hooks across tests

✅ Use an autouse fixture.

Related: @tool decorator reference, Agent class reference, and the first Crew tutorial.

clear_all_global_hooks() FAQ

What is clear_all_global_hooks() in CrewAI?

Removes every registered crew-scoped hook — essential for test isolation. CrewAI registers many hook types — before and after LLM calls, before and after tool calls, plus programmatic register_* helpers — in shared global registries so decorators and runtime plugins can compose without threading state through every constructor. That ergonomics trade-off means tests and REPL sessions can leak hooks into later cases unless you explicitly reset. clear_all_global_hooks() drops every registered callable so the next import or test starts from a clean sla…

Which CrewAI types expose the method clear_all_global_hooks()?

DevShelfHub documents clear_all_global_hooks() on Hooks lifecycle. The reference maps it to Python module crewai.hooks — pin your installed crewai version and match imports to the snippet on this page.

When should I use clear_all_global_hooks()?

Test setup, hot-reload setups.

When should I avoid clear_all_global_hooks()?

Production code paths — leave hooks installed.

How do I call clear_all_global_hooks() from Python?

clear_all_global_hooks()

Where can I explore more CrewAI API reference pages?

Open the CrewAI API reference index on DevShelfHub to search classes, methods, and decorators, each with runnable examples, parameters, common mistakes, and cross-links.