Data Management

Bitmapist provides several utility functions to help you manage your event data and the temporary keys created by bitwise operations.

Discovering Event Names

If you need to get a list of all event names stored in Redis, you can use get_event_names().

from bitmapist import get_event_names

# Get all event names
all_events = get_event_names()
print(f"Found events: {all_events}")

# Get event names with a specific prefix
# This is useful for namespaced events like 'task:completed', 'task:created'
task_events = get_event_names(prefix='task:')
print(f"Task-related events: {task_events}")

This function scans the Redis keyspace for keys matching the trackist_* pattern and extracts the event names. It automatically filters out temporary bit operation keys.

Deleting Event Data

Deleting a Specific Period

Every event object (e.g., DayEvents, MonthEvents) has a .delete() method that removes the Redis key associated with that specific period.

from bitmapist import DayEvents

# Delete all 'login' events for yesterday
yesterday_logins = DayEvents('login').prev()
yesterday_logins.delete()

Deleting All Events

To completely wipe all bitmapist data from a Redis database (including all event data and temporary keys), use delete_all_events().

Warning: This is a destructive operation and cannot be undone. Use with caution.

from bitmapist import delete_all_events

# Delete all keys starting with 'trackist_'
delete_all_events()

Managing Temporary Bit Operation Keys

Bitwise operations create temporary keys in Redis to store their results. These keys are prefixed with trackist_bitop_. While they can be useful for caching results, they can also accumulate over time. Bitmapist offers two ways to clean them up.

delete_runtime_bitop_keys()

This is the recommended and safest method. It deletes only the temporary keys that were created by bitwise operations within the current thread since the last cleanup.

It's a good practice to call this at the end of a request or a script that performs bit operations.

from bitmapist import DayEvents, delete_runtime_bitop_keys

active_today = DayEvents('active')
premium = UniqueEvents('premium')

# This operation creates a temporary key
result = active_today & premium
print(len(result))

# Clean up the key created by the operation above
delete_runtime_bitop_keys()

delete_temporary_bitop_keys()

This function scans the entire Redis keyspace for keys matching trackist_bitop_* and deletes them all.

Warning: This can be a slow and resource-intensive operation on a Redis instance with millions of keys. It can also interfere with other processes that might be using cached bit operation results. It is generally better to use delete_runtime_bitop_keys().

from bitmapist import delete_temporary_bitop_keys

# Deletes ALL bitop keys, regardless of when or where they were created
delete_temporary_bitop_keys()