This document explains how to set up periodic cleanup of temporary files generated by the Math2Visual API.
The Math2Visual API generates temporary files and database records that need periodic cleanup:
- Visualization Output (
storage/output/): Temporary SVG files for visualization requests using unique UUIDs to prevent conflicts in parallel requests. - SVG Generation (
storage/temp_svgs/): Temporary AI-generated SVG icons that haven't been confirmed by the user yet. - Tutor Sessions (database): Expired tutor session records stored in PostgreSQL that are no longer active.
These files and records are not cleaned up immediately to avoid blocking the API. Instead, a periodic cleanup job removes old files and expired sessions.
app/utils/cleanup.py- Cleanup utility classes and functionsscripts/cleanup_temp_files.py- CLI script for manual or scheduled cleanupdocs/cleanup_setup.md- This documentation
You can run the cleanup script manually:
# Clean up with defaults (output files >24h, temp SVGs >1h)
cd backend
python scripts/cleanup_temp_files.py
# Clean up output files older than 1 hour
python scripts/cleanup_temp_files.py --age-hours 1
# Clean up temp SVG files older than 30 minutes
python scripts/cleanup_temp_files.py --temp-svg-age-hours 0.5
# Dry run to see what would be cleaned (both directories)
python scripts/cleanup_temp_files.py --dry-run
# Show storage statistics (both directories)
python scripts/cleanup_temp_files.py --statsTo set up automated cleanup, add a cron job:
crontab -e# Recommended: Clean up every hour (output files >24h, temp SVGs >1h)
0 * * * * cd /path/to/math2visual/backend && python scripts/cleanup_temp_files.py >> /var/log/math2visual_cleanup.log 2>&1
# Alternative: Clean up every 6 hours with custom thresholds
0 */6 * * * cd /path/to/math2visual/backend && python scripts/cleanup_temp_files.py --age-hours 24 --temp-svg-age-hours 2 >> /var/log/math2visual_cleanup.log 2>&1
# More aggressive: Clean up temp SVGs every 30 minutes
*/30 * * * * cd /path/to/math2visual/backend && python scripts/cleanup_temp_files.py --temp-svg-age-hours 0.5 >> /var/log/math2visual_cleanup.log 2>&1chmod +x backend/scripts/cleanup_temp_files.pyformal_*-*-*-*-*.svg- Temporary formal visualization files (formal_{uuid}.svg)intuitive_*-*-*-*-*.svg- Temporary intuitive visualization files (intuitive_{uuid}.svg)
*.svg- All temporary AI-generated SVG icon files
- Default: 24 hours
- Configurable via
--age-hoursparameter - Files older than this threshold are removed
- Default: 1 hour
- Configurable via
--temp-svg-age-hoursparameter - Shorter threshold since these are meant to be very temporary (user-aborted generations)
- Default: 2 hours of inactivity
- Fixed expiration time (not configurable via CLI)
- Sessions are automatically expired based on
last_activitytimestamp - Only cleaned up when database is available (gracefully skips if database is unavailable)
- Required for proper operation with multiple Gunicorn workers (sessions are shared via database)
Archive files are only created when running in debug mode (FLASK_DEBUG=true):
formal_{timestamp}_{uuid}.svgintuitive_{timestamp}_{uuid}.svg
To include archive files in cleanup, use the --include-archives flag:
python scripts/cleanup_temp_files.py --include-archivesThis will also clean up:
formal_*_*-*-*-*-*.svg- Archive formal files (formal_{timestamp}_{uuid}.svg)intuitive_*_*-*-*-*-*.svg- Archive intuitive files (intuitive_{timestamp}_{uuid}.svg)
python scripts/cleanup_temp_files.py --statstail -f /var/log/math2visual_cleanup.log# Check what files exist in output directory
ls -la storage/output/
# Check what files exist in temp SVG directory
ls -la storage/temp_svgs/
# Check file ages in output directory
find storage/output/ -name "*.svg" -type f -exec ls -la {} \;
# Check file ages in temp SVG directory
find storage/temp_svgs/ -name "*.svg" -type f -exec ls -la {} \;If cleanup fails due to permissions:
# Ensure the cleanup script can write to both directories
chmod 755 storage/output/ storage/temp_svgs/
chown -R www-data:www-data storage/output/ storage/temp_svgs/ # Adjust user/group as neededIf storage is filling up:
- Reduce the age thresholds:
--age-hours 6 --temp-svg-age-hours 0.5 - Run cleanup more frequently in cron (e.g., every 30 minutes)
- Check for very large files:
find storage/output/ -size +10M find storage/temp_svgs/ -size +10M
- Monitor both directories:
python scripts/cleanup_temp_files.py --stats
- Check cron service:
systemctl status cron - Check cron logs:
grep CRON /var/log/syslog - Test script manually:
python scripts/cleanup_temp_files.py --dry-run
If tutor session cleanup is not working:
- Check database connection: Ensure
DATABASE_URLis set correctly in your environment - Verify database is running: Check that PostgreSQL is accessible
- Check cleanup logs: Look for "Could not clean up tutor sessions" messages
- Manual verification: Sessions expire after 2 hours of inactivity - check
tutor_sessionstable in database:SELECT session_id, last_activity, NOW() - last_activity AS age FROM tutor_sessions ORDER BY last_activity DESC;
- Note: Tutor session cleanup gracefully skips if database is unavailable (won't cause errors)
- Minimal: Cleanup runs outside of API requests
- Storage efficient: Prevents disk space issues
- Non-blocking: API responses are not delayed by cleanup
- Configurable: Age thresholds can be tuned based on usage patterns
- Cleanup only removes files matching specific patterns
- Archive files with timestamps are preserved by default
- Cleanup errors are logged but don't affect API functionality
- Temp SVG files are cleaned more aggressively (1 hour default) to prevent accumulation from aborted generations
-
Visualization Output (
storage/output/):- Created during normal API operations
- Persist longer (24h) for debugging and caching
- May be referenced by active sessions
-
Temp SVG Directory (
storage/temp_svgs/):- Created when users generate AI SVG icons
- Only needed until user confirms or cancels
- Should be cleaned quickly (1h) to handle:
- User-aborted generations
- Browser crashes/closes
- Network interruptions
- Race conditions between frontend and backend