I just wrote a small plugin that moves all unused files to a trash directory. USE AT OWN RISK. Make sure you edit the *** areas with your own settings.
<?php
/*
Plugin Name: WPDM Dead File Checker
Plugin URI: https://wordpress.org/plugins/wpdm_check
Description: Make a list of valid documents.
Version: 1
Author: Colin T
Author URI: http://maniacalventures.com
License: GPLv2 or later
Text Domain: wpdm_check
*/
$dwhtk = new wpdm_check;
class wpdm_check {
var $wp_prefix = '****';
var $file_dir = '***\wp-content\uploads\download-manager-files';
var $trash_dir = '***\wp-content\uploads\download-manager-files-trash';
function __construct() {
if( !is_dir( $this->trash_dir ) ) {
mkdir( $this->trash_dir );
}
add_action( 'admin_menu', [ $this, 'activate_page' ] );
}
function doit() {
global $wpdb;
$query = " SELECT po.ID, pm.meta_value FROM {$this->wp_prefix}_posts po
LEFT JOIN {$this->wp_prefix}_postmeta pm ON po.ID = pm.post_id
WHERE post_type LIKE 'wpdmpro' and meta_key LIKE '__wpdm_files'";
$raw = $wpdb->get_results( $query );
$valid_list = [];
foreach( $raw as $val ) {
$valid_list += unserialize( $val->meta_value );
}
sort( $valid_list );
$real_list = [];
foreach( scandir( $this->file_dir ) as $val ) {
if( $val == '.' or $val == '..' ) continue;
$real_list[] = $val;
}
$difference = array_diff( $real_list, $valid_list );
foreach( $difference as $val ) {
echo "Trashing: ".$val."<br>\r\n";
rename( $this->file_dir .'\\'. $val, $this->trash_dir.'\\'.$val );
}
}
function activate_page() {
add_menu_page( 'WPDM Check', 'WPDM Check', 'read', 'wpdm_check_management', [ $this, 'doit' ], '', 200 );
}
}