Skip to content

v2.2.0 - Media Cleanup

Choose a tag to compare

@KarlosSchmidtos KarlosSchmidtos released this 30 Apr 08:12
· 43 commits to main since this release

🗑️ Media Cleanup Job

Cleans up orphaned media files after every sync. Prevents /wp-content/immoadmin/media/ from accumulating data garbage when units are deleted or images replaced.

What's new

  • Auto-cleanup on every sync: scans the media directory and deletes files no longer referenced by any unit
  • Admin dashboard: shows count of cleaned files in sync log + live progress (🗑️ N aufgeräumt)
  • Defensive: try/catch around cleanup so it can never break the sync
  • Safe: skips hidden files (.htaccess, .DS_Store)

How it works

  1. After all units are synced (and orphan posts deleted), one indexed SQL query collects all currently referenced media URLs
  2. Filenames are extracted via basename(parse_url(...)) into a lookup set
  3. Directory scan compares each file → unreferenced files are deleted, all events logged via error_log()

Edge cases handled

  • Multiple units referencing the same file (kept)
  • Failed downloads stored as remote URL fallback (no file on disk → no false delete)
  • Non-ASCII filenames (sanitized consistently on both sides)

Performance

One indexed SQL + one glob() per sync. Negligible overhead even for large sites.