Support » Fixing WordPress » delete 100,000 images from media library

  • Resolved eduguytoo


    I have a odd issue in a wordpress installation where there appears to be over 100,000 images, in the media library, I am trying to find out how I can locate these files on the server, and delete them all.

    PHP has a time out issue, so using mass delete, returns an error so thats not a good method, deleting 300 images at a time is not a good option imagine how long that would take.

    I have deleted all the images in the uploads folder, however apparently the database still holds references to these files, can someone point me to a method of using PHP admin to delete the references to these images.

    Any suggestions would be welcomed.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Keep in mind that deleting files from the server via SSH, FTP, etc. will not remove the entries for them in the WordPress media library.

    Do this if you want to clear the entire media library on the the server and in WordPress (remember to back everything up first):

    Browse to the uploads folder, then do rm -rf *. Wait until all is clear and the uploads folder is empty.

    I’ll edit this shortly with the database solution.

    yes, I see that it is still showing references in the database, thanks for the information, I appreciate that. I looked at some of the tables but I could not identify for sure which ones hold the reference data.

    You may be able to run a quick command in phpMyAdmin that removes all entries containing a post_type of “attachment”.

    Locate your WordPress database in phpMyAdmin, then run this SQL command to delete ALL attachments:

    DELETE FROM `wp_posts` WHERE `post_type` = "attachment"

    As always, use with caution. This coupled with the SSH command should get you fixed up.

    Thanks, yes, that worked saved me a ton of time.

    For anyone else reading this – be aware that depending on plugins used, you will have at least one, maybe 4 or more records on the wp_postmeta table. It is wise to delete these two, as if you don’t they end up being orphaned in the database, and taking up space / slowing things down.

    So, first:

    DELETE FROM wp_postmeta
    WHERE post_id IN
    SELECT id
    FROM wp_posts
    WHERE post_type = ‘attachment’

    Then once executed,

    DELETE FROM wp_posts WHERE post_type = ‘attachment’

    I just deleted 8,400 images, after deleting 25,000+ rows in wp_postmeta – if you don’t do this and your site is or will possibly be big in the future, you may run into performance issues…


    I would like to recommend the plugin WP Media Cleaner.
    It helps me testing on local server big sites with hundred of thousands of pictures. It handles retina and woocommerce products as well.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘delete 100,000 images from media library’ is closed to new replies.