H3XED

How to Bulk Delete all Images in WordPress Media Library and Database

Sep 30, 2017   Web Development   Nick Vogt   Comments (7)
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.


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":

WordPress Bulk Delete

Then click the Screen Options button (near the top), which will be visible now:

WordPress Bulk Delete

Change the number of items per page to 200 and hit Apply (newer versions of WordPress accept up to 999, so try that first):

WordPress Bulk Delete

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:

WordPress Bulk Delete


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";
Share This Post
Twitter
WooCommerce Beginner Manual
WooCommerce Beginner Manual
See More Details
Professional WordPress Design
Professional WordPress Design
See More Details

Comments (7)

Rich   Jun 04, 2018
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!
Seb   May 30, 2018
Yep, have to say. I was looking for this article for a while. Glad you put it together, thanks.
Peter   May 28, 2018
Well done! Great article!
kickers   May 20, 2018
Saved me a lot of time. T.Y.
Ellis Benus Web Developer   May 09, 2018
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!
Jared   Apr 02, 2018
Thanks for the write up
Francisco   Mar 23, 2018
Thanks! This saved me a lot of time. Mass delete WP images.
Share This Post
Twitter
WooCommerce Beginner Manual
WooCommerce Beginner Manual
See More Details
Professional WordPress Design
Professional WordPress Design
See More Details
H3XED © Nick Vogt   RSS   Policies   Twitter