[+] Delete translated images from media library

It is not possible to delete translated images from the media library with our current WPML Version.
You can use this code snippet to delete translated images from the library.

NOTE:- Make sure you have a complete database and files backup of the site before proceeding

Add this code to theme functions.php file


add_action('init', function() {
	if (isset($_GET['wpml_media_cleanup'])) {
		set_time_limit(0);
		global $wpdb;
		$ids_to_fix = $wpdb->get_results("SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE element_type = 'post_attachment' AND source_language_code IS NOT NULL", ARRAY_A);
		if (!empty($ids_to_fix) && is_array($ids_to_fix)) {
			foreach ($ids_to_fix as $row_data) {
				if (!empty($row_data['element_id'])) {
					$wpdb->query($wpdb->prepare("DELETE FROM $wpdb->posts WHERE $wpdb->posts.ID = %d", $row_data['element_id']));
					$wpdb->query($wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE $wpdb->postmeta.post_id = %d", $row_data['element_id']));
				}
			}
			wp_die('Done!');
		} else {
			wp_die('No media translation found!');
		}
	}
});

Visit the site URL with wpml_media_cleanup in query string e.g. http://example.com/?wpml_media_cleanup
Once done you can reset the WPML if you are planning to delete WPML or Remove ghost entries from the translation tables. Then remove the code snippet from the theme.


Support Thread Example

Let us know if this snippet is not working for you:

This snippet doesn’t work
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments