skip to Main Content

My media has over 100k images. I filtered it out by name and deleted it but limited to 200 pictures, if more would get the error "URI Too Long error". So can you show me how to batch delete photos by name in Media? I have used plugins like Media Cleaner, WPS cleaner but it doesn’t work, it takes a lot of time. I use WordPress Bitnami on Google cloud.
Thanks for the help.
problem description photo

2

Answers


  1. You can achieve it by using below script.

    Please make sure before running this script take a database & full site backup.

    Script

    global $wpdb;
    
    $filename_pattern = 'loc_va_phu_tung'; //pattern of the file/media which you want to delete.
    $lastmedia_id = get_option('wh_last_checked_media_id', 0); //flag to detect the last media that was deleted.
    $limit = 20; //number if files that you want to delete per request
    
    /**
     * Get list of all pending image to delete
     *
     * @global $wpdb
     * @param int $lastmedia_id last media ID
     * @param string $filename_pattern file pattern
     * @param int $limit number of files that you want to delete
     * @return array
     */
    function wh_get_image_list($lastmedia_id, $filename_pattern, $limit) {
        global $wpdb;
        return $wpdb->get_results($wpdb->prepare('SELECT `ID`, `post_date`, `post_title`, `post_excerpt`, `post_status`, `post_name`, `post_parent`, `guid`, `post_type`, `post_mime_type`
                                                   FROM `' . $wpdb->prefix . 'posts`
                                                    WHERE `post_name` LIKE %s
                                                    AND `post_type` = %s
                                                    AND `ID` > %d
                                                   LIMIT  %d', '%' . $filename_pattern . '%', 'attachment', $lastmedia_id, $limit), OBJECT);
    }
    
    $data = wh_get_image_list($lastmedia_id, $filename_pattern, $limit);
    print_r($wpdb->last_query, 1); //To check the query which was executed.
    
    if (!empty($data)) {
        foreach ($data as $media) {
            print_r($media, 1, 'del');
    
            # Uncomment below line if you want to delete the media permanently
            # Once the file is deleted you will not be able to restore it unless you have a backup.
            # wp_delete_attachment($media->ID, true);
        }
    
        update_option('wh_last_checked_media_id', $media->ID, false);
    }
    

    How to use it

    You can create a custom template and put the above code. Then create a page using newly created template then visit the URL. It will display the list of media that is deleted. You can also setup a CRON which will do the stuff in the background.

    The code is tested, and fully functional.

    Login or Signup to reply.
  2. If you have SSH access to your VM instance running WordPress then you can connect to it, cd into /bitnami/wordpress/wp-content/ (this is where the files are located when you use Bitnami solution) and delete files manually by running rm command.

    rm *common_file_name_part*

    If for some reason files are not where they should you can locate them with find / -xdev 2>/dev/null -name "wp-content".

    Here you can find some practical examples how to use find.

    Before deleting anything I’d recommend doing a copy of the directory you’re in. Or even creating a snapshot of the entire VM’s disk.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search