Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
Find Unused SFS IDs
This binary compares UUIDs from the email_sfs_mappings table against a file of used UUIDs to identify which SFS mappings are no longer referenced in email message bodies. It streams data for memory efficiency and can handle millions of records.
Purpose
Part of a three-step workflow to identify and clean up unused SFS files:
find_used_sfs_ids→ Finds SFS UUIDs referenced in email message bodies- This tool → Compares all mappings against used UUIDs to find orphans
cleanup_unused_sfs→ Deletes the unused files and mappings
How It Works
- Loads the used UUIDs file into a HashSet for O(1) lookups
- Streams SFS mapping destinations from database (if not cached)
- Extracts UUIDs from destination URLs
- Writes mapping UUIDs to
all_sfs_mapping_uuids.txt(cached for future runs) - Streams through all mapping UUIDs and compares against used set
- Writes unused UUIDs directly to output file
Memory Efficient: Only the used UUIDs HashSet is kept in memory. All other operations stream data line-by-line.
Environment Variables
Required
DATABASE_URL: PostgreSQL connection string for the MacroDB database
Optional
USED_UUIDS_FILE: Path to file containing used UUIDs (default:used_sfs_uuids.txt)- Generated by
find_used_sfs_ids
- Generated by
ALL_MAPPINGS_FILE: Path to store all mapping UUIDs (default:all_sfs_mapping_uuids.txt)- Cached file to avoid re-querying database
UNUSED_UUIDS_FILE: Path to store unused UUIDs (default:unused_sfs_uuids.txt)EXTRACTION_CONCURRENCY: Concurrency for UUID extraction (default:100)- Not currently used due to streaming refactor
Usage
Basic Usage
cargo run --bin find_unused_sfs_ids
With Custom Configuration
export DATABASE_URL="postgresql://user:pass@localhost/macrodb"
export USED_UUIDS_FILE="custom_used_uuids.txt"
export UNUSED_UUIDS_FILE="custom_unused_uuids.txt"
cargo run --bin find_unused_sfs_ids
From Release Binary
./find_unused_sfs_ids
Input File
used_sfs_uuids.txt (required)
Must be generated first by running find_used_sfs_ids. Contains one UUID per line.
Example:
bc698c53-5a61-45f9-ac88-212e22cf8a33
d4e7f1a2-9b3c-4d2e-8f5a-1a2b3c4d5e6f
Output Files
unused_sfs_uuids.txt (default)
Contains one UUID per line for each SFS mapping that is NOT referenced in any email message.
Example:
e1f2a3b4-c5d6-4e7f-8a9b-0c1d2e3f4a5b
f2a3b4c5-d6e7-4f8a-9b0c-1d2e3f4a5b6c
all_sfs_mapping_uuids.txt (default)
Cached list of all UUIDs from email_sfs_mappings table. If this file exists on subsequent runs, the database query is skipped entirely (much faster).
To force a fresh database query, delete this file.
Caching Behavior
The tool uses aggressive caching to speed up repeated runs:
First run: Queries database for all mappings (~slow)
- Streams mappings from
email_sfs_mappingstable - Writes UUIDs to
all_sfs_mapping_uuids.txt
- Streams mappings from
Subsequent runs: Uses cached file (~instant)
- Counts lines in
all_sfs_mapping_uuids.txt(streaming) - Skips database connection entirely
- Counts lines in
To refresh the cache (e.g., after new email mappings are created):
rm all_sfs_mapping_uuids.txt
cargo run --bin find_unused_sfs_ids
Performance
- Streaming architecture: Never loads all mappings or UUIDs into memory
- Memory usage: Only used UUIDs HashSet in memory (~1MB per 100k UUIDs)
- Processing speed: ~100k-500k UUIDs per second on comparison
- Database streaming: Fetches mapping destinations one-by-one via SQL cursor
Typical performance:
- Loading used UUIDs: ~0.1-1s for 10k-100k UUIDs
- Streaming from database (first run): ~10-60s for 1M mappings
- Counting cached file (subsequent runs): ~1-5s for 1M mappings
- Finding unused UUIDs: ~2-10s for 1M mappings
Can handle tens of millions of records without running out of memory.
Example Run
First Run (No Cache)
=== Find Unused SFS IDs ===
Loading configuration...
Configuration:
Used UUIDs file: used_sfs_uuids.txt
All mappings file: all_sfs_mapping_uuids.txt
Unused UUIDs file: unused_sfs_uuids.txt
Extraction concurrency: 100
Loading used UUIDs from used_sfs_uuids.txt...
Loaded 12847 used UUIDs in 145ms
Connecting to the database...
Streaming mapping destinations from email_sfs_mappings to all_sfs_mapping_uuids.txt...
Streamed 10000 UUIDs so far...
Streamed 20000 UUIDs so far...
...
Streamed and wrote 234567 UUIDs in 45.2s
Streaming through all_sfs_mapping_uuids.txt to find unused UUIDs...
Processed 100000 UUIDs, found 87234 unused so far...
Processed 200000 UUIDs, found 187654 unused so far...
Found 221720 unused UUIDs in 3.8s
=== Summary ===
Total mappings: 234567
Total mapping UUIDs: 234567
Total used UUIDs (from file): 12847
Total unused UUIDs: 221720
Percentage unused: 94.52%
Results saved to: unused_sfs_uuids.txt
Subsequent Run (With Cache)
=== Find Unused SFS IDs ===
Loading configuration...
Configuration:
Used UUIDs file: used_sfs_uuids.txt
All mappings file: all_sfs_mapping_uuids.txt
Unused UUIDs file: unused_sfs_uuids.txt
Extraction concurrency: 100
Loading used UUIDs from used_sfs_uuids.txt...
Loaded 12847 used UUIDs in 145ms
All mappings file already exists at all_sfs_mapping_uuids.txt, using cached data...
Counted 234567 UUIDs in file in 1.2s
Streaming through all_sfs_mapping_uuids.txt to find unused UUIDs...
Processed 100000 UUIDs, found 87234 unused so far...
Processed 200000 UUIDs, found 187654 unused so far...
Found 221720 unused UUIDs in 3.8s
=== Summary ===
Total mappings: 234567
Total mapping UUIDs: 234567
Total used UUIDs (from file): 12847
Total unused UUIDs: 221720
Percentage unused: 94.52%
Results saved to: unused_sfs_uuids.txt
Next Steps
After running this tool:
- Review the percentage of unused files (high percentage is normal)
- Optionally spot-check a few unused UUIDs to verify they're safe to delete
- Run
cleanup_unused_sfsto delete the unused files and database mappings
Troubleshooting
"File does not exist: used_sfs_uuids.txt"
- Run
find_used_sfs_idsfirst to generate this file
"Failed to connect to database"
- Only occurs on first run (database is skipped if cache exists)
- Verify
DATABASE_URLis set correctly - Check database is running and accessible
- Ensure user has SELECT permissions on
email_sfs_mappingstable
Out of memory
- Unusual with streaming architecture
- Check size of
used_sfs_uuids.txt- this is the only file loaded fully into memory - If used UUIDs file is extremely large (>10M entries), you may need more RAM
Results look wrong (unexpected percentage)
- Verify
used_sfs_uuids.txtwas generated from the same database - Check that
all_sfs_mapping_uuids.txtis up-to-date (delete and re-run if unsure) - Ensure destination URLs in database follow expected format:
{prefix}/{uuid}
Want to force fresh database query
rm all_sfs_mapping_uuids.txt
cargo run --bin find_unused_sfs_ids