Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
Find Used SFS IDs
This binary scans email message HTML content to find all SFS (Static File Service) UUIDs that are referenced in message bodies. This helps identify which SFS mappings are actually in use versus those that are orphaned.
Purpose
Part of a three-step workflow to identify and clean up unused SFS files:
- This tool → Finds SFS UUIDs referenced in email message bodies
find_unused_sfs_ids→ Compares all mappings against used UUIDs to find orphanscleanup_unused_sfs→ Deletes the unused files and mappings
How It Works
- Fetches all email message IDs from the database
- Retrieves message HTML content in batches (streaming, memory-efficient)
- Extracts SFS UUIDs from HTML using regex pattern matching
- Writes unique UUIDs to output file as they're discovered
- Supports resume functionality - if interrupted, it picks up where it left off
Environment Variables
Required
DATABASE_URL: PostgreSQL connection string for the MacroDB database
Optional
SFS_DOMAIN: The domain to search for (default:static-file-service.macro.com)MESSAGE_IDS_FILE: Path to store/load message IDs (default:message_ids.txt)USED_UUIDS_FILE: Path to store found UUIDs (default:used_sfs_uuids.txt)FETCH_BATCH_SIZE: Number of messages to fetch from DB per query (default:1000)BATCH_SIZE: Number of messages to process before logging progress (default:1000)PREFETCH_BATCHES: Number of batches to prefetch while processing (default:2)
Usage
Basic Usage
cargo run --bin find_used_sfs_ids
With Custom Configuration
export DATABASE_URL="postgresql://user:pass@localhost/macrodb"
export SFS_DOMAIN="static-file-service-dev.macro.com"
export USED_UUIDS_FILE="custom_used_uuids.txt"
cargo run --bin find_used_sfs_ids
From Release Binary
./find_used_sfs_ids
Output Files
used_sfs_uuids.txt (default)
Contains one UUID per line for each SFS file referenced in email message bodies.
Example:
bc698c53-5a61-45f9-ac88-212e22cf8a33
d4e7f1a2-9b3c-4d2e-8f5a-1a2b3c4d5e6f
a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d
message_ids.txt (default)
Cached list of all message IDs from the database. If this file exists, the tool will use it instead of querying the database again.
message_ids.processed (automatically created)
Tracks which message IDs have been processed. Used for resume functionality.
Resume Functionality
The tool automatically supports resuming interrupted runs:
- On start, checks for existing
message_ids.processedfile - Skips messages that have already been processed
- Appends newly found UUIDs to the existing output file
- Progress is saved incrementally, so interruptions are safe
To force a fresh run, delete:
message_ids.processedused_sfs_uuids.txt(optional, if you want to start fresh)
Performance
- Streaming architecture: Fetches and processes messages in batches to avoid loading all into memory
- Concurrent processing: Uses tokio tasks to process multiple messages in parallel
- Prefetching: Database batches are fetched ahead of processing to minimize wait time
- Incremental writes: UUIDs are written to disk as they're found, not buffered in memory
Typical performance:
- ~1000-2000 messages per second (varies based on HTML complexity)
- Memory usage stays constant regardless of total message count
- Can process millions of messages without issue
Example Run
=== Find Used SFS IDs ===
Loading configuration...
Configuration:
SFS Domain: static-file-service.macro.com
Message IDs file: message_ids.txt
Used UUIDs file: used_sfs_uuids.txt
Fetch batch size: 1000
Progress batch size: 1000
Prefetch batches: 2
Connecting to the database...
Fetching all message IDs from the database...
Found 145623 message IDs
Total message IDs to process: 145623
[Batch 1/146] DB fetch: 1000 messages in 245ms
[Batch 1/146] Processing: 1000 messages in 1.2s
Progress: 1000/145623 messages processed, 342 unique UUIDs found (342 new this run)
[Batch 2/146] DB fetch: 1000 messages in 198ms
[Batch 2/146] Processing: 1000 messages in 1.1s
Progress: 2000/145623 messages processed, 687 unique UUIDs found (345 new this run)
...
=== Processing Complete ===
Total messages processed: 145623
Total unique SFS UUIDs found: 12847
Results saved to: used_sfs_uuids.txt
Next Steps
After running this tool:
- Run
find_unused_sfs_idsto compare all mappings against this list - Review the unused UUIDs file to verify they're safe to delete
- Run
cleanup_unused_sfsto delete the unused files and mappings
Troubleshooting
"Failed to connect to database"
- Verify
DATABASE_URLis set correctly - Check database is running and accessible
- Ensure user has SELECT permissions on email_messages table
Out of memory
- Reduce
FETCH_BATCH_SIZE(try 500 or 250) - Reduce
PREFETCH_BATCHES(try 1)
Slow performance
- Increase
FETCH_BATCH_SIZE(try 2000 or 5000) - Ensure database has proper indexes on email_messages table
- Check database connection pool size
Resume not working
- Check that
message_ids.processedfile has write permissions - Verify file is in the same directory as the binary