Support » Fixing WordPress » Delete ALL 30,000 anattached images in Media library – pluggin?

  • Is there a way to delete 30,000 unattached images from my media library?

    I know I can select ‘unattached’ and then click the top check box to tick all the boxes on one page and delete page by page, but my server is soooo slow (maybe cause I never cleaned it up). Plus the maximum number of images I can delete at one go is only 300 (URL length restriction).

    Is there a plugin / script / spell I can use for this?
    Spent about 5 hours deleting 1/4 of them page by page (ok.. I was watching TV as well.. but still killing me).


Viewing 8 replies - 1 through 8 (of 8 total)
  • plus.. i keep deleting the images I want when wordpress times out and goes back to the ALL list!!! grrr.



    Forum Moderator

    The fact that you are trying to delete 300 images at a time is what is probably causing the time-outs.

    maybe.. its just as slow if i do 50 at a time.
    Just want them deleted.

    Can it be done in phpMyadmin somehow?



    Forum Moderator

    In theory, yes if you are handy with creating custom sql queries. But you would still need to remove all of the unwanted files via FTP.

    yeah. I’m not handy at all 🙁



    Forum Moderator

    Not that I’m aware of. 🙁


    Did you ever find a convenient way to delete all the unattached images?

    There seems to be a big need for an all in one “cleanup” plugin that does something like this:
    – remove all unattached images after verifying that they are not used somewhere on the site
    – delete the actual image files from the uploads folder that are not linked to from any post
    – do this unattended in the background

    While we are at it, we might as well have an option to delete all trash and any items from the list below that is not being used by a published, pending or draft post or page:
    – taxonomy terms
    – attachments
    – user_meta
    – post_meta
    – comments
    – any other dead weight from both the database and filesystem.

    Call it “spring cleaning” 🙂 I see the wp-cleanup plugin, which is a good start, but I don’t think it does anything with images.

    I personally have a need for this because I’m working on an import program that gets all my data and images from a couple of existing sites and maps it all and inserts new posts and taxonomy terms on one new site. I just spent the whole day deleting all the imported stuff because of a design change to the new site. But I could not just do a reinstall since there is a significant amount of data on the new site I wanted to keep. Hmmm … maybe that will be my first plugin.

    I have a workaround for this… it’s a bit messy, but it works. You need to have phpMyAdmin access for the database. And obviously all the usual “OMG, don’t do that” warnings apply – take backups, don’t blame me if it fails, etc. 🙂

    I just got rid of over 200,000 unattached media items on one of my blogs by this method.

    1. Install the DX Delete Attached Media plugin (

    2. Create a new post. I named mine “Unattached media” and saved it as draft.

    3. Find the post number. Just go to the posts list, click the post title and look at the URL: it will have “post=xxxxx” in it.

    3. Run the SQL in your phpMyAdmin interface:

    UPDATE wp_posts
    SET post_parent = [post number of your new post]
    WHERE post_type =  'attachment'
    AND post_parent = 0

    4. Go back to your posts list and send your post to the trash.

    5. Make sure the DX plugin is activated, empty trash. Bingo, all dead items gone.

    Note that if there are a LOT of items, you might have to just leave the post in the trash and wait for WP’s standard trash emptying procedures to handle it. But it still works.

    Hope it helps!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Delete ALL 30,000 anattached images in Media library – pluggin?’ is closed to new replies.