Skip to content

Instantly share code, notes, and snippets.

@Soullighter
Last active March 8, 2024 13:27
Show Gist options
  • Save Soullighter/d0ad66cff63ecf0bc1d0f5dc881d9dd3 to your computer and use it in GitHub Desktop.
Save Soullighter/d0ad66cff63ecf0bc1d0f5dc881d9dd3 to your computer and use it in GitHub Desktop.
How to delete all Media Files / Gallery from Database SQL in WordPress
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.
@pipetko
Copy link

pipetko commented Feb 14, 2023

I am working on a website generated with WP Staging plugin. Problem is... I cant find those tables. I dont want to use this query if it will delete the original website's tables too.

If you want to delete only Media files that are uploaded over WP Admin, this is the code that you need. It will delete every Media that is uploaded there.

OK thanks :)

@xplication
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment