How to Bulk Delete all Images in WordPress Media Library and Database
Sep 30, 2017Web DevelopmentComments (25)
There are two options to completely delete all media in your WordPress media library in bulk. One using the WordPress admin page (safer) and one performing direct deletions using FTP/SSH/PHPMyAdmin, which is good if you have thousands of items to delete.
Following the below steps can be very dangerous. I don't recommend doing this on a production/live website. Be sure to backup everything first.
You can delete up to 200 items in your media library at a time using this method (now up to 999 with the latest version of WordPress). First, log in to your admin and go to the media library. Change the view to "line details":
Then click the Screen Options button (near the top), which will be visible now:
Change the number of items per page to 200 and hit Apply (newer versions of WordPress accept up to 999, so try that first):
Now you will have many more items displayed per page of the media library. Use the "all" checkbox and bulk option to delete a full page of images at a time:
This method allows you to delete all items in your media library at once. Be sure to backup your database prior to doing this.
Each file in your media library has one row in the `wp_posts` table and two rows in the `wp_postmeta` table. Learn more about how WordPress stores media library entries in the database here. You can delete them all in bulk using these commands:
You can run those in PHPMyAdmin or by logging into MySQL through SSH. I won't cover how to log in to either of those platforms here. If you're unsure, ask your hosting provider.
Files
Now that the database entries are gone, use FTP or SSH to delete the files themselves. They reside in your uploads folder:
Please note that many plugins add their own folders to the uploads folder. You probably don't want to delete those. By default WordPress stores your uploaded media library files in /year/month/ folders.
If you have WooCommerce you may find that your products still think they have an image and gallery images after doing the above steps. To remove all image remnants from the WooCommerce products, you'll need to run these SQL commands as well:
Following the below steps can be very dangerous. I don't recommend doing this on a production/live website. Be sure to backup everything first.
Using WordPress Admin
You can delete up to 200 items in your media library at a time using this method (now up to 999 with the latest version of WordPress). First, log in to your admin and go to the media library. Change the view to "line details":
Then click the Screen Options button (near the top), which will be visible now:
Change the number of items per page to 200 and hit Apply (newer versions of WordPress accept up to 999, so try that first):
Now you will have many more items displayed per page of the media library. Use the "all" checkbox and bulk option to delete a full page of images at a time:
Using FTP/SSH & Database
This method allows you to delete all items in your media library at once. Be sure to backup your database prior to doing this.
Each file in your media library has one row in the `wp_posts` table and two rows in the `wp_postmeta` table. Learn more about how WordPress stores media library entries in the database here. You can delete them all in bulk using these commands:
DELETE FROM `wp_posts` WHERE `post_type` = "attachment";
DELETE FROM `wp_postmeta` WHERE `meta_key` = "_wp_attached_file";
DELETE FROM `wp_postmeta` WHERE `meta_key` = "_wp_attachment_metadata";
You can run those in PHPMyAdmin or by logging into MySQL through SSH. I won't cover how to log in to either of those platforms here. If you're unsure, ask your hosting provider.
Files
Now that the database entries are gone, use FTP or SSH to delete the files themselves. They reside in your uploads folder:
/wp-content/uploads
Please note that many plugins add their own folders to the uploads folder. You probably don't want to delete those. By default WordPress stores your uploaded media library files in /year/month/ folders.
WooCommerce Product Images
If you have WooCommerce you may find that your products still think they have an image and gallery images after doing the above steps. To remove all image remnants from the WooCommerce products, you'll need to run these SQL commands as well:
DELETE FROM `wp_postmeta` WHERE `meta_key` = "_thumbnail_id";
UPDATE `wp_postmeta` SET `meta_value` = NULL WHERE `meta_key` = "_product_image_gallery";
Thanks. Saved ma a lot of time.
amazing thank so much
Thanks, It worked so well me. God bless you
Thank you so much... You saved my life huh!! You ROCK
The below SQL code works great. What is the best SQL code for deleting only unattached images in media file in WordPress - anyone have the code for just these unattached images I wish to remove? Thanks Kim
Thankyou for this info - this is amazing and has saved me hours of time. Appreciate your help here :) Kim
Not working for me. The WooCommerce one didn't find anything
Fantastic. Just cleared out all media!
I wanted to do something similar to what @matt suggested, but delete images by date. Alas, it required several further steps: 1. Because I want to retain a lot of posts (just delete the older ones), the first thing was to get a list of attachments before a certain date, e.g. SELECT ID FROM wp_posts WHERE post_type = "attachment" AND post_date < "YYYY-MM-DD" 2. The names of the physical files to actually delete afterwards are stored in wp_postmeta under guid. So I saved the result of SELECT guid FROM wp_postmeta WHERE post_date < "YYYY-MM-DD" 3. Then I ran @matt's modified query (using post_date) 4. Using the IDs from step 1, I deleted all posts matching those IDs. (I used a temporary table) 5. Then I ran a shell script to delete each file on the result saved under 2.
The problem with this is that it deletes off all the photos you have used in your blog posts so if you do a mass delete then you have no pics left in your posts!
hello, the code helped me a lot Can you help me with a code to search for woocommerce products with duplicate title and remove it? My email is johanavendano1@gmail.com Thank you
Thank you so much, was so helpful.
This is no help. SInce after 300 entries the url will be too long. Fail.
https://wordpress.org/plugins/remove-media-library/
I've been looking for ages for an explanation on how the media library, the WordPress files and the database entries handle media files in context. This is a really great and very helpful post. Thanks for that.
To avoid deleting everything one could do something like: DELETE pm FROM `wp_postmeta` pm LEFT JOIN `wp_posts` p ON pm.post_id=p.ID WHERE (meta_key='_wp_attached_file' OR meta_key='_wp_attachment_metadata') AND p.post_title REGEXP '^[A-Z][A-Z][0-9]+$'; (the extra condition being after the last AND) and AFTER THAT delete the wp_posts as in the article, for the example above: DELETE FROM `wp_posts` WHERE `post_type` = "attachment" AND post_title REGEXP '^[A-Z][A-Z][0-9]+$'; And finally delete the files - Use at your own risk and WITH CAUTION
Thank you! I wish I would have googled this sooner, rather than looking for a plugin!
Are those photos getting deleted from wp-content/uploads as well or we need to delete the folders manually? You didn't mention that
This is awesome! Any way to do that where we can say "prior to X date" ? I deleted years of posts of my website because the DB was huge, I was able to delete the posts, but I dont think the attachments were deleted. So I want to see if I could delete all that attachment info from the DB. I can easily delete the actual files from the server, but from the DB is different. Thanks!
Yep, have to say. I was looking for this article for a while. Glad you put it together, thanks.
Well done! Great article!
Saved me a lot of time. T.Y.
Thank you for sharing. I deleted all the image files (several thousand) but I KNEW the database had to be just cluttered with extra data. This is exactly what i was looking for. Thanks for the help!
Thanks for the write up
Thanks! This saved me a lot of time. Mass delete WP images.