Skip to Content
← Back to all demos

Storage Management

Manage localStorage events with clearStoredEvents(), including date-based filtering and count limits

clearStoredEvents() APIKeep last N eventsClear before dateCombined filtersDate/ISO/timestamp inputClear all storage

Storage Status

0
events in memory
(updates every 500ms)

Clear All Events

Remove all stored events while keeping the session active.

// Clear all stored events
widget.store.clearStoredEvents();

Keep Last N Events

Clear older events but keep the most recent N events.

5
// Keep only the last 5 events
widget.store.clearStoredEvents(5);

Clear Events Before Date (Relative)

Clear events older than a specified time period.

1h ago
// Clear events older than 1 hour
const cutoff = Date.now() - 1 * 60 * 60 * 1000;
widget.store.clearStoredEvents({ beforeDate: new Date(cutoff) });

Clear Events Before Date (ISO String)

Clear events before a specific date using an ISO 8601 string or timestamp.

// Clear events before a specific date (ISO string)
widget.store.clearStoredEvents({
  beforeDate: '2026-01-14T12:00:00.000Z'
});

// Also accepts Unix timestamp (milliseconds)
widget.store.clearStoredEvents({
  beforeDate: 1736856000000
});

Combined: beforeDate + keepCount

Apply both filters: first remove events before the date, then apply keepCount to the remaining events.

Older than 1h
+
Keep max 5
// Combined: date filter + keep count
// 1. First removes events before the date
// 2. Then keeps only the last 5 of remaining events
widget.store.clearStoredEvents({
  beforeDate: new Date(Date.now() - 1 * 60 * 60 * 1000),
  keepCount: 5
});

Clear All Storage (Nuclear Option)

Completely remove all localStorage data for this experience. This clears session, events, state, and disclaimer acceptance. The widget will need to be reinitialized.

// Static method - no widget instance needed
const success = NexusChatWidget.clearStorage("your-experience-id");

// Clears: session, events, state, disclaimer acceptance
// Returns: true if cleared, false if localStorage unavailable

Action Log

Actions will appear here...
Try the controls on the left!

API Reference

// Type definitions
interface ClearEventsOptions {
  keepCount?: number;                    // Keep at least N most recent events
  beforeDate?: Date | string | number;   // Clear events before this date
}

// Method signature (supports both forms for backward compatibility)
store.clearStoredEvents(options?: number | ClearEventsOptions): void

// Behavior:
// - No arguments: clears all stored events
// - Number: keeps the last N events (backward compatible)
// - { beforeDate }: clears events with timestamp < beforeDate
// - { keepCount }: keeps last N events
// - { beforeDate, keepCount }:
//     1. First removes events before beforeDate
//     2. Then if remaining > keepCount, keeps only last keepCount

// Date parameter accepts:
// - Date object: new Date('2026-01-14')
// - ISO 8601 string: '2026-01-14T12:00:00.000Z'
// - Unix timestamp (ms): 1736856000000

// Note: Events with timestamp >= beforeDate are preserved

Use Cases

  • Clear history button - Let users clear their chat history with clearStoredEvents()
  • Retain recent context - Clear old messages but keep recent ones with clearStoredEvents(10)
  • Daily cleanup - Clear messages older than 24 hours on page load
  • Session expiry - Clear old conversations after a period of inactivity
  • Privacy compliance - Automatically clear data older than retention period
  • User logout - Clear all storage with NexusChatWidget.clearStorage() when user signs out