Software Developer

Delete All Attachments of Specified Date in WordPress Database

These are MySQL statements to bulk delete all WordPress attachments of a specified modified date in the WordPress database. Both steps are needed to remove not only the attachments, but also the postmeta which is attached to the parent post. This is a clean way to get rid of attachments without leaving behind dangling postmeta.

In this example, I use a wildcard (LIKE ‘2014-%’) for the date. This example will delete all attachments that have the year 2014 as the date. You can change the ‘2014-%’ in both steps to meet your needs.

Warning: Use this at your own risk. You may accidentally delete items that you do not intend to delete.

STEP 1

DELETE FROM wp_postmeta
WHERE post_id IN
(
SELECT id
FROM wp_posts
WHERE post_modified LIKE '2014-%'
AND post_type = 'attachment'
)

STEP 2

DELETE FROM wp_posts WHERE post_modified LIKE '2014-%' AND post_type = 'attachment'

By

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]