2025-11-23 09:19:06 -03:00
# CLEAR KB
2025-11-22 16:12:32 -03:00
2025-11-23 09:19:06 -03:00
Remove knowledge bases from the current session's context.
2025-11-22 16:12:32 -03:00
## Syntax
```basic
2025-11-23 09:19:06 -03:00
CLEAR KB kb_name
2025-11-22 16:12:32 -03:00
```
2025-11-23 09:19:06 -03:00
or to clear all:
2025-11-22 16:12:32 -03:00
```basic
2025-11-23 09:19:06 -03:00
CLEAR KB ALL
2025-11-22 16:12:32 -03:00
```
## Parameters
| Parameter | Type | Description |
|-----------|------|-------------|
2025-11-23 09:19:06 -03:00
| `kb_name` | String | Name of the knowledge base to remove, or "ALL" for all KBs |
2025-11-22 16:12:32 -03:00
## Description
2025-11-23 09:19:06 -03:00
The `CLEAR KB` keyword removes previously loaded knowledge bases from the session's active context. This frees up memory and ensures that subsequent searches don't include unwanted collections.
2025-11-22 16:12:32 -03:00
## Examples
### Clear Specific Knowledge Base
```basic
2025-11-23 09:19:06 -03:00
USE KB "product-docs"
USE KB "user-manuals"
' Later, remove just product-docs
CLEAR KB "product-docs"
' Now only user-manuals is active
2025-11-22 16:12:32 -03:00
```
### Clear All Knowledge Bases
```basic
2025-11-23 09:19:06 -03:00
' Load multiple KBs
USE KB "policies"
USE KB "procedures"
USE KB "faqs"
2025-11-22 16:12:32 -03:00
' Clear everything
2025-11-23 09:19:06 -03:00
CLEAR KB ALL
TALK "All knowledge bases have been unloaded"
2025-11-22 16:12:32 -03:00
```
2025-11-23 09:19:06 -03:00
### Conditional Clearing
2025-11-22 16:12:32 -03:00
```basic
2025-11-23 09:19:06 -03:00
topic = HEAR "What topic interests you?"
2025-11-22 16:12:32 -03:00
IF topic = "technical" THEN
2025-11-23 09:19:06 -03:00
CLEAR KB "marketing-docs"
USE KB "technical-docs"
ELSE IF topic = "sales" THEN
CLEAR KB "technical-docs"
USE KB "marketing-docs"
2025-11-22 16:12:32 -03:00
END IF
```
2025-11-23 09:19:06 -03:00
### Switch Knowledge Context
2025-11-22 16:12:32 -03:00
```basic
2025-11-23 09:19:06 -03:00
' Start with general KB
USE KB "general-info"
answer = FIND "company overview"
2025-11-22 16:12:32 -03:00
TALK answer
2025-11-23 09:19:06 -03:00
' Switch to specific department
CLEAR KB "general-info"
USE KB "engineering-specs"
answer = FIND "API documentation"
TALK answer
2025-11-22 16:12:32 -03:00
```
2025-11-23 09:19:06 -03:00
### Memory Management
```basic
' Load KBs for initial context
USE KB "onboarding"
USE KB "policies"
2025-11-22 16:12:32 -03:00
2025-11-23 09:19:06 -03:00
' After onboarding complete
CLEAR KB "onboarding"
' Keep policies active but free onboarding memory
```
2025-11-22 16:12:32 -03:00
2025-11-23 09:19:06 -03:00
## Return Value
2025-11-22 16:12:32 -03:00
2025-11-23 09:19:06 -03:00
Returns `true` if the KB was successfully cleared, `false` if the KB wasn't loaded or doesn't exist.
2025-11-22 16:12:32 -03:00
## Error Handling
2025-11-23 09:19:06 -03:00
```basic
result = CLEAR KB "unknown-kb"
IF result = false THEN
LOG "KB was not loaded or doesn't exist"
END IF
```
2025-11-22 16:12:32 -03:00
## Performance Considerations
2025-11-23 09:19:06 -03:00
- Clearing KBs immediately frees session memory
2025-11-23 17:02:22 -03:00
- Does not delete the actual KB from vector database
2025-11-23 09:19:06 -03:00
- Only removes the session association
- Clearing all KBs is faster than clearing individually
2025-11-22 16:12:32 -03:00
## Best Practices
2025-11-23 09:19:06 -03:00
1. **Clear Unused KBs** : Remove KBs when no longer needed
```basic
' After processing department-specific queries
CLEAR KB "department-kb"
```
2. **Clear Before Loading** : Ensure clean state
```basic
CLEAR KB ALL
USE KB "fresh-context"
```
3. **Memory Optimization** : Clear large KBs after use
```basic
USE KB "large-archive"
results = FIND query
CLEAR KB "large-archive" ' Free memory
```
4. **Context Switching** : Clear when changing topics
```basic
ON TOPIC_CHANGE
CLEAR KB ALL
USE KB new_topic_kb
END ON
```
## Session Scope
- Clearing only affects the current session
- Other sessions maintain their own KB associations
2025-11-23 17:02:22 -03:00
- KBs remain in vector database for future use
2025-11-23 09:19:06 -03:00
- Can reload cleared KBs anytime with `USE KB`
## Monitoring Active KBs
2025-11-22 16:12:32 -03:00
```basic
2025-11-23 09:19:06 -03:00
' Check what's loaded before clearing
active_kbs = GET_ACTIVE_KBS()
TALK "Currently loaded: " + JOIN(active_kbs, ", ")
' Clear specific ones
FOR EACH kb IN active_kbs
IF kb STARTS WITH "temp_" THEN
CLEAR KB kb
END IF
NEXT
```
## Advanced Usage
### Batch Operations
```basic
kbs_to_clear = ["old-docs", "archive-2022", "deprecated"]
FOR EACH kb IN kbs_to_clear
CLEAR KB kb
NEXT
```
### Scheduled Cleanup
```basic
' Clear all KBs at conversation timeout
ON TIMEOUT
CLEAR KB ALL
LOG "Session KBs cleared due to timeout"
END ON
```
### Conditional Preservation
```basic
' Clear all except essential KBs
all_kbs = GET_ACTIVE_KBS()
essential = ["core-policies", "emergency-procedures"]
FOR EACH kb IN all_kbs
IF kb NOT IN essential THEN
CLEAR KB kb
END IF
NEXT
2025-11-22 16:12:32 -03:00
```
## Related Keywords
2025-11-23 09:19:06 -03:00
- [USE KB ](./keyword-use-kb.md ) - Load knowledge bases
- [ADD WEBSITE ](./keyword-add-website.md ) - Create KB from website
2025-11-22 16:12:32 -03:00
- [FIND ](./keyword-find.md ) - Search within loaded KBs
2025-11-23 09:19:06 -03:00
- [LLM ](./keyword-llm.md ) - Use KB context in responses
2025-11-22 16:12:32 -03:00
2025-11-23 09:19:06 -03:00
## Implementation
2025-11-22 16:12:32 -03:00
Located in `src/basic/keywords/clear_kb.rs`
The implementation:
2025-11-23 09:19:06 -03:00
- Maintains session KB registry
- Removes KB references from context
- Updates search scope
- Handles "ALL" keyword specially
- Returns operation status