2

We have large media library with more than 300000 images.

We decide to delete all images < 2017 year manually using "FTP", from WP upload folder.

But after that in WP Admin > Media > Library, they still "exist" as screenshot - broken media links in library

We tried with couple of plugins , but there isn't any with option to bulk delete images by month or year.

Is there any way to speed up the process, instead manually deleting it using "filter by year" and "Pagination Number of items per page" show: 999 , where we always get "request timeout".

2 Answers 2

3

We tried with couple of plugins , but there isn't any with option to bulk delete images by month or year.

If you have access to wp-cli you can try to delete attachments by year and month with:

wp post delete $(wp post list --post_type='attachment' -—year=2016 -—monthnum=12 --format=ids)

or just by year with:

wp post delete $(wp post list --post_type='attachment' -—year=2016 --format=ids)

Remember to backup before testing!!

1
  • Make sure to add --force at the end of the query as attachments can't be sent to trash!
    – Maxime
    Oct 26, 2022 at 19:15
1

Your images still exist in your Media Library because they are stored in the database, just like posts and pages. You can delete an image directly via FTP, but that doesn't magically delete its database row.

Have you looked in the database yet?

You have two options:

(1) Open up your database directly, go to your _posts table, filter by post_type (you will need attachment) and bulk delete the rows from there.

(2) Create a function that gets all your attachments (from 2017 and back) and delete them accordingly. You can use get_posts() for this. Loop through the returned array and delete them automatically by using wp_delete_post().

Which option to choose is up to you. When this is a one time deal and you have direct access to your database, option (1) would probably be the fastest. If you want to automatically delete all attachments older than X years, then you are better off creating a function for it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.