This is only available if you are using the PRO version of Media Cleaner.
It’s not possible to automate the process of cleaning. It would be way too dangerous, uncontrollable, and would also slow down your install at every visit. I actually advise you against scheduling automatic process directly on WordPress. However, you can do this using WP-CLI, and that will not slow down your install as it will be executed in a different environment.
If you are the admin of your server or if your hosting services offer you SSH access and WP-CLI, you can run Media Cleaner from the command line. This also brings new possibilities like running a scan every once in a while, getting the results by e-mail, etc.
Available Commands
wp media-cleaner scan
Performs a comprehensive scan to identify unused media files in your WordPress installation.
Basic Usage:
wp media-cleaner scan
Expected Output:
* Method : Media Library
* Check Content : yes
* Cache Enabled : yes
* Memory Limit : 512M
* Current Memory : 45.23MB
> Reset Issues : Resetting issues and references...
> Analyze Content ( 1000 posts/batch ): 100% [============================] 2,450/2,450 (0:02:15)
> List Medias : 100% [============================] 1,234/1,234 (0:00:45)
> Check Usage : 100% [============================] 1,234/1,234 (0:01:30)
Found 23 issues:
- [#45] Media 234: uploads/2023/01/unused-image.jpg (Not used)
- [#46] Media 567: uploads/2023/02/old-banner.png (Not used)
- [#47] Media 891: uploads/2023/03/deprecated-logo.svg (Not used)
Options:
debug– Enable debug mode for detailed outputfilesystem– Use filesystem method instead of media librarymedia– Force media library methodcheck-media– Include media library check (filesystem mode only)uncheck-media– Skip media library checkcheck-content– Include content analysisuncheck-content– Skip content analysis
wp media-cleaner issues
Lists all detected issues (unused media files) that haven’t been ignored or deleted.
Basic Usage:
wp media-cleaner issues
Expected Output:
Found 23 issues:
- [#45] Media 234: uploads/2023/01/unused-image.jpg (Not used)
- [#46] Media 567: uploads/2023/02/old-banner.png (Not used)
- [#47] Media 891: uploads/2023/03/deprecated-logo.svg (Not used)
- [#48] Media 123: uploads/2023/04/redundant-photo.jpg (Not used)
No Issues Output:
No issues found.
wp media-cleaner delete
Deletes unused media files. Can delete all issues or specific items by ID.
Delete All Issues:
wp media-cleaner delete
Delete Specific Items:
wp media-cleaner delete 45 46 47
Expected Output:
- Deleted Media 234 (uploads/2023/01/unused-image.jpg): Not used
- Deleted Media 567 (uploads/2023/02/old-banner.png): Not used
- Could not delete Media 891 (uploads/2023/03/deprecated-logo.svg): Not used
wp media-cleaner trash
Lists all media files that have been deleted (moved to trash).
Basic Usage:
wp media-cleaner trash
Expected Output:
Found 5 items:
- [#45] Media 234: uploads/2023/01/unused-image.jpg (Not used)
- [#46] Media 567: uploads/2023/02/old-banner.png (Not used)
wp media-cleaner recover
Recovers deleted media files from trash. Can recover all items or specific ones by ID.
Recover All Deleted Items:
wp media-cleaner recover
Recover Specific Items:
wp media-cleaner recover 45 46
Expected Output:
- Recovered Media 234 (uploads/2023/01/unused-image.jpg): Not used
- Recovered Media 567 (uploads/2023/02/old-banner.png): Not used
- Could not recover Media 891 (uploads/2023/03/deprecated-logo.svg): Not used
wp media-cleaner empty
Permanently removes all items from trash (empties the trash).
Basic Usage:
wp media-cleaner empty
Expected Output:
- Trashed Media 234 (uploads/2023/01/unused-image.jpg): Not used
- Trashed Media 567 (uploads/2023/02/old-banner.png): Not used
Advanced Usage Examples
Filter Issues by Filename Pattern
Extract issue IDs for images containing “tokyo” in the filename:
wp media-cleaner issues | grep tokyo | sed -n 's/\- \[#\([0-9]*\)\].*/\1/p'
Expected Output:
45
67
89
Delete Only Specific File Types
Find and delete only PNG files:
# Get PNG issue IDs
png_ids=$(wp media-cleaner issues | grep '\.png' | sed -n 's/\- \[#\([0-9]*\)\].*/\1/p' | tr '\n' ' ')
# Delete them
wp media-cleaner delete $png_ids
Scan with Filesystem Method and Debug
Perform a detailed filesystem scan with debug information:
wp media-cleaner scan filesystem debug check-content
Expected Debug Output:
:: Debug mode enabled ::
* Method : Filesystem
* Check Library : no
* Check Content : yes
* Cache Enabled : yes
* Memory Limit : 512M
* Current Memory : 45.23MB
> Reset Issues : Resetting issues and references...
> Analyze Content : Processing 1000 posts...
> Memory Status : Current: 67.45MB, Peak: 89.12MB (after 5000 items)
> List Files : Processing uploads/2023/01/image1.jpg
> List Files : Processing uploads/2023/01/image2.png
> Memory Status : Current: 78.23MB, Peak: 95.67MB (after 1000 items)
Batch Processing with Memory Monitoring
For large sites, monitor memory usage during scans:
# Start scan in background and monitor memory
wp media-cleaner scan debug &
scan_pid=$!
# Monitor memory while scanning
while kill -0 $scan_pid 2>/dev/null; do
echo "Memory: $(free -h | awk '/^Mem:/ {print $3 "/" $2}')"
sleep 30
done
Extract and Process Issue Data
Get detailed issue information in CSV format:
wp media-cleaner issues | grep -E '^\- \[#' | sed 's/\- \[#\([0-9]*\)\] Media \([0-9]*\): \(.*\) (\(.*\))/\1,\2,\3,\4/' > unused_media.csv
Conditional Deletion Based on Date
Delete only files from a specific year:
# Get issues from 2022
wp media-cleaner issues | grep '2022' | sed -n 's/\- \[#\([0-9]*\)\].*/\1/p' | while read id; do
wp media-cleaner delete $id
done
Safe Deletion Workflow
Recommended workflow for safe media cleanup:
# 1. Perform scan
wp media-cleaner scan debug
# 2. Review issues
wp media-cleaner issues > issues_backup.txt
# 3. Delete in small batches
wp media-cleaner issues | head -10 | sed -n 's/\- \[#\([0-9]*\)\].*/\1/p' | while read id; do
wp media-cleaner delete $id
done
# 4. Check trash
wp media-cleaner trash
# 5. If everything looks good, empty trash
wp media-cleaner empty
Memory-Conscious Large Site Scanning
For very large WordPress installations:
# Set higher memory limit and scan in filesystem mode
php -d memory_limit=1G $(which wp) media-cleaner scan filesystem debug check-content