• Hello,

    I′ve set to Opx medium and large image sizes in Media tab in de Administration Screen, and unset in functions.php theme′s file the largest ones being generated since the last WordPress updates. But I′d like to remove from db all sizes but both the uploaded as featured image and thumbnails.

    In another time, I removed the unused ones directly from the file manager in cpanel and had to reedit post/pages since the removal deteled displayed images on the site. And had to upload them all again with different names.

    1) I′d like to know if there′s a safe command to run or function to add to remove the unwanted sizes. I′ve found a function here using wp_get_attachment_image but I don′t understand how to search in all posts/pages for the sizes I want to remove by applying a filter, and then remove them.

    2) A time ago I manually deleted somme of the images′ metadata from db (wp_attachment_metadata meta key in wp_postmeta table), since I didn′t find a command or any other way to do it. I′d like to know if this could be done manually by deleting other elements of that array, or would that also remove images from being displayed as before?

    Regards,

    Julia

    • This topic was modified 3 years, 1 month ago by Jan Dembowski.
    • This topic was modified 3 years, 1 month ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Everything else WordPress topic
Viewing 13 replies - 1 through 13 (of 13 total)
  • Hey @julia77 ??

    >>I′d like to know if there′s a safe command to run or function to add to remove the unwanted sizes<<

    #You can use this to delete existing unwanted image sizes: https://www.ads-software.com/plugins/regenerate-thumbnails/
    #You can use this to remove unused/broken media entries:
    https://www.ads-software.com/plugins/media-cleaner/

    #In order to stop WordPress from generating image size for new new images, Put this in your theme’s functions.php file:

    add_filter( 'intermediate_image_sizes_advanced', 'wpse_240765_image_sizes' );
    
    function wpse_240765_image_sizes( $sizes ){
        $sizes = array();
        return $sizes;
    }

    Apparently, there is a plugin for this too:
    https://www.ads-software.com/plugins/image-sizes/

    >>I′d like to know if this could be done manually by deleting other elements of that array, or would that also remove images from being displayed as before?<<

    Here is something that might be helpful: https://www.ads-software.com/support/topic/automatic-remove-of-useless-attachment-metadata/

    Let me know how it goes!

    Thread Starter julia77

    (@julia77)

    Hello @soberbanda,

    I′ve started a topic in Regenerate thumbnails plugin′s support forum to check if old images sizes are deleted when regenerating according to new settings in WordPress Administration Screen, and to know what happens to old metadata stored in db. When I get an answer, I′ll post it in here.

    In that forum, I′ve also read that other people deleted unused images directly from the server. And even after regenerating with the plugin, metadata in db remained as before and images on the website were not properly displayed (metadata was pointing to images that no longer existed). That happened to me years ago, as I mentioned when started this topic.

    In the thread you provided, they say:

    – Size data in WordPress is stored as a serialized array.
    – Convert data to normal arrays and back to serialized automatically.
    – Query results in PHP to get metadata where the meta_value has a string like one of the size names you want to delete.
    – Step through the records and unset the undesired size elements.
    – Save the modified array back to the DB.

    I have no idea how to do this, I just found the code mentioned at the beginning of this topic.

    About applying filters when uploading images, I′m currently using this line in functions.php file, specifically for the new hughes sizes since one of the latest WordPress update. When combined with values set in the Administration Screen, it works fine up to now:

    remove_filter( ‘wp_robots’, ‘wp_robots_max_image_preview_large’ );

    Which is the difference with your code?

    `add_filter( ‘intermediate_image_sizes_advanced’, ‘wpse_240765_image_sizes’ );

    function wpse_240765_image_sizes( $sizes ){
    $sizes = array();
    return $sizes;
    }

    In here, I′ve found your code with parameters:

    apply_filters( ‘intermediate_image_sizes_advanced’, array $new_sizes, array $image_meta, int $attachment_id )

    Regards,

    Julia

    >>And even after regenerating with the plugin, metadata in db remained as before and images on the website were not properly displayed (metadata was pointing to images that no longer existed)<<

    @julia77 I won’t disagree to this. It can very likely pose a serious challenge if you have a lot of images on your website.

    You may want to try this (manual but failproof):

    1. Export your database with PHPMyAdmin
    2. Open it’s copy (always make backup) in Notepad++
    3. Press CTRL+H to open Replace dialogue
    4. Put “sizes”.*.”image_meta” into “Find what: ” field
    5. Put “sizes”;a:0:{}s:10:”image_meta” into “Replace with: ” field
    6. Tick “Regular expression”, untick “. matches newline”
    7. Click “Find Next” button and make sure only the desired part of a string is selected
    8. If it is, click “Replace All” button
    9. Import your updated database with PHPMyAdmin

    >>remove_filter( ‘wp_robots’, ‘wp_robots_max_image_preview_large’ );<<

    It lets search engines decide which image size to use for previews. I don’t see how that would stop WordPress from generating new image sizes.

    intermediate_image_sizes_advanced filter lets you unset the default WP image sizes that are generated whenever a user uploads a new image to the media library.

    I just dig a little deeper into this and found that intermediate_image_sizes_advanced may not work for some people. Try reviewing: https://wordpress.stackexchange.com/questions/357955/wp-5-3-removing-default-wordpress-image-sizes

    The easier way to do this is to put the default image generation in media setting to zero. Here is how that would look like: https://i.stack.imgur.com/7VTBX.png

    let me know ??

    Thread Starter julia77

    (@julia77)

    About the remove_filter directive, I don′t understand either how it prevents WordPress from generating those hughe images. All I know is that they′re not being generated since then…as there′s no option in the Admin panel to unset them, I guess that directive is working somehow …

    I′m about to delete images′ sizes metadata manually via exporting/importing a query for only _wp_attachment_metadata meta_key in wp_postmeta table (not the whole table or db).

    After that, then I can delete unused images′ sizes without deleting the ones being currently displayed on the site, right?

    >>About the remove_filter directive, I don′t understand either how it prevents WordPress from generating those hughe images<<

    @julia77 Go through this: https://www.wpbeginner.com/news/whats-coming-in-wordpress-5-7-features-and-screenshots/

    >>as there′s no option in the Admin panel to unset them, I guess that directive is working somehow<<

    The easier way to do this is to put the default image generation in media setting to zero. Here is how that would look like: https://i.stack.imgur.com/7VTBX.png<&lt;

    >>After that, then I can delete unused images′ sizes without deleting the ones being currently displayed on the site, right?<<

    #Yeah but first I recommend using this: https://www.ads-software.com/plugins/media-cleaner/ to clear out broken media entries.

    Also, try and get a backup. Just the SQL file.

    Have a good day ??

    Thread Starter julia77

    (@julia77)

    Just one question about replacing a WordPress table in database: I′ve downloaded wp_postmeta table in a PHP file format. After modifying data, should I just delete the table in db and then upload the modified data in the same file format?

    I′ve never deleted or replaced a WordPress table in db before

    @julia77 Yes you can but keep a backup incase things go wrong (with WordPress that happens way too often).

    You might want to go through this as well: https://www.wpbeginner.com/beginners-guide/beginners-guide-to-wordpress-database-management-with-phpmyadmin/

    Go ahead ??

    Thread Starter julia77

    (@julia77)

    Hi @soberbanda,

    I have my wp_postmeta table ready to replace the old one. Before that, I`ve activated the free version of Media Cleaner plugin to see settings and options to run after replacing the table. Unfortunately, the plugin says that additional checks are required to scan posts/pages built with Elementor (that′s the builder on my entire website).

    Would you recommend any other way to check for errors in images′ displaying before deleting the unused sizes?

    >>Would you recommend any other way to check for errors in images′ displaying before deleting the unused sizes?<<

    @julia77 You can use this: https://www.ads-software.com/plugins/broken-link-checker/

    Good luck ??

    Thread Starter julia77

    (@julia77)

    Finally, I did it manually, directly editing each row in the table in phpMyAdmin. And checked with Broken Link Checker plugin.

    Thanks @soberbanda for your answers!!

    Since that table uses to be so big, and most users just upload and delete and change themes/media sizes and reload and change again, etc. sooo often …could someone at WordPress just give a customizable function to handle data in that table? Specifically for images …I′m using a filter to prevent storing exif and iptc metadata in the same meta key for sizes:

    add_filter( ‘wp_read_image_metadata’, ‘__return_false’ );

    but I had to delete manually all data that was added previously by default …

    I guess wp-includes/media.php points something about this but I have no idea about serializing, unserializing, filtering results, deleting specific data, serializing again and restoring to db …I′m affraid this tasks have to be done manually by users like me …??

    • This reply was modified 3 years, 1 month ago by julia77.
    • This reply was modified 3 years, 1 month ago by julia77.

    >>could someone at WordPress just give a customizable function to handle data in that table? Specifically for images <<

    @julia77 That would be great. I recommend suggesting it to the developers here: https://core.trac.www.ads-software.com/

    >>I guess wp-includes/media.php points something about this but I have no idea about serializing, unserializing, filtering results, deleting specific data, serializing again and restoring to db<<

    You will need to write a custom php script for this. Manually deleting image size meta data directly from SQL is never a great idea. I would suggest hiring a freelancer for the task.

    >>Finally, I did it manually, directly editing each row in the table in phpMyAdmin. And checked with Broken Link Checker plugin.<<

    Kudos to you and best of luck with the project ??

    Thread Starter julia77

    (@julia77)

    I will suggest this, thanks for the link.

    Kudos to you and best of luck with the project

    What is kudos? What project?

    >>What is kudos? What project?<<

    @julia77 It is an informal way to say congratulations. And project is the one you are working on right now, your website: https://www.tresmancias.com/.

    I am sorry I need to work on my English ??

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Remove unsed image sizes’ is closed to new replies.