Skip Navigation

This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

This topic contains 24 replies, has 2 voices.

Last updated by Mihai Apetrei 1 year, 8 months ago.

Assisted by: Mihai Apetrei.

Author Posts
March 25, 2023 at 8:51 am #13328367

Phil Cox

Tell us what you are trying to do?
Our DEV site is suffering from duplicated media (44,000 files at the last count).

Your documentation suggests we do not need WPML media translation for this site under development. How do we remove WPM Media translation?

Is there any documentation that you are following?
https://wpml.org/documentation/getting-started-guide/media-translation/?utm_source=plugin&utm_medium=gui&utm_campaign=wpmlcore

Is there a similar example that we can see?

What is the link to your site?
hidden link

March 25, 2023 at 8:54 am #13328369

Phil Cox

Please could you include this address in responses: phil@avalonmarketing.co.nz.
Thanks.

March 25, 2023 at 4:18 pm #13329489

Mihai Apetrei
Supporter

Languages: English (English )

Timezone: Europe/Bucharest (GMT+02:00)

Hi there.

Please take a look over the solution presented in the following two tickets below:

- https://wpml.org/forums/topic/remove-media-duplicates/

- https://wpml.org/forums/topic/wpml-media-remove/

That should help you fix this.

Please let me know how things go.

Kind regards,
Mihai Apetrei

March 27, 2023 at 8:14 pm #13342893

Phil Cox

Hi again WPML support team!

I have received your clear instructions for removing excess media entries from the database.
I note that there are 12 languages enabled on the DEV site.

For each language, I have attempted to remove all media as advised by WPML. However, the media page claims that there are 44,790 media entries in the database for each of the 12 languages. Clearly it isn’t practical to delete these in batches of 100. Please could you advise a more effective way of deleting these database entries in bulk? Can it be done through direct access to the database using phpmyAdmin?

Many thanks,
Phil. (phil@avalonmarketing.co.nz)

Screen Shot 2023-03-28 at 9.06.54 AM.png
Screen Shot 2023-03-28 at 9.04.27 AM.png
March 27, 2023 at 10:09 pm #13343391

Mihai Apetrei
Supporter

Languages: English (English )

Timezone: Europe/Bucharest (GMT+02:00)

Sure, that's an option, too.

Please take a look here (https://wpml.org/forums/topic/delete-all-media-translations/) and here (https://wpml.org/forums/topic/delete-all-duplicated-media-not-manually/#post-4097245).

I recommend that you first create a full website backup for safety reasons.

Mihai

March 28, 2023 at 12:29 am #13343969

Phil Cox

Thanks for this.
I have followed the fix you published.

I ran the following code against the database:

SELECT element_id
FROM wp_icl_translations
WHERE
element_type = 'post_attachment'
AND
source_language_code IS NOT NULL

The result is a table of 537,516 entries.
The fix suggests I run the following code against that table:

$wpdb->query($wpdb->prepare("DELETE FROM $wpdb->posts WHERE $wpdb->posts.ID = %d", $attachment->element_id));$wpdb->query($wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE $wpdb->postmeta.post_id = %d", $attachment->element_id));

Please could you clarify how to do this? Do I add a snippet to the website or is this some sort of offline script to prepare the data?

Thanks again,
Phil.
phil@avalonmarketing.co.nz

Screen Shot 2023-03-28 at 1.29.16 PM.png
March 30, 2023 at 8:21 pm #13370241

Mihai Apetrei
Supporter

Languages: English (English )

Timezone: Europe/Bucharest (GMT+02:00)

Hi Phil.

This would be done via PHP.

Please take a look here:
hidden link

You can also try this:
https://wpml.org/forums/topic/script-to-delete-all-media-translations-please/#post-2018583

This is more of a custom request that you can try but only with a backup done first.

The recommended way is to use the WordPress backend for removing the media content, as each media element that you upload to WordPress is connected to various tables, and running an incomplete query could lead to unexpected DB entries that were missing or forgotten to get removed while running the queries.

Mihai

March 30, 2023 at 10:13 pm #13370757

Phil Cox

Very many thanks, Mihai,
I am unable to try this until probably Tuesday, so I'll let y ou know how it goes.

This is clearly a capability that should be shipped with WPML, but I accept that we are having to use a workaround instead. I shall be careful!
Kind regards,
Phil.

April 2, 2023 at 5:59 pm #13385057

Mihai Apetrei
Supporter

Languages: English (English )

Timezone: Europe/Bucharest (GMT+02:00)

Yes, Phil.

Please let me know how things go.

April 10, 2023 at 12:13 pm #13437141

Phil Cox

Thank you.

Sorry, we still have not had an opportunity to try this fix. We'll let you know how it goes when we do. Thanks,

April 10, 2023 at 5:34 pm #13439179

Mihai Apetrei
Supporter

Languages: English (English )

Timezone: Europe/Bucharest (GMT+02:00)

No worries, Phil.

Thank you for the update.

Get back to us when you can.

If the ticket gets closed at some point, just open a new one.

Have a lovely rest of the day!

Mihai

April 16, 2023 at 1:28 am #13479895

Phil Cox

Hi Mihai, sorry for the delay. I was away for longer than intended and was unable to pursue the solution you presented, until now.

I have now been through your response. I have addressed each of your points below:

1. Please take a look here:
hidden link

This is an interesting introduction to using the $wpdb class to interact with the WordPress database. The article will be very useful to programmers interested in writing queries for a WordPress database. It does not address the problem we are having with WPML.

2. You can also try this:
https://wpml.org/forums/topic/script-to-delete-all-media-translations-please/#post-2018583

This article proposes a solution by Carlos Rojas which isn't accepted by the customer, who instead has to develop his own solution. Sadly the customer's solution isn't provided.

3. "This is more of a custom request."

Actually no. It's a fairly standard request to disable something WPML has done. What is surprising is that it seems to require a custom solution to do it. After all these years, is there still no workable, published solution to undo WPML media library bloat?

Kind regards,
Phil.

April 19, 2023 at 1:33 am #13500485

Mihai Apetrei
Supporter

Languages: English (English )

Timezone: Europe/Bucharest (GMT+02:00)

Hi Phil.

Let's try this (I just tested it on a local environment and it worked fine).

NOTE: It would be best if you can test it on a staging environment or at least make sure you have a complete database and files backup of the site before proceeding

1. Add this code to the currently active theme's 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!');
		}
	}
});

2. Visit the site URL with wpml_media_cleanup in query string (e.g. hidden link)

3. Once done > Remove ghost entries from the translation tables:
https://wpml.org/documentation/support/wpml-troubleshooting-options/

Then remove the code snippet from the theme.

Please let me know if that worked out fine for you.

Mihai

April 20, 2023 at 8:19 pm #13514247

Phil Cox

Thanks for sharing this solution, Mihai. I followed the steps you provided but at step 2., it crashes the site: "There has been a critical error on this website.".

Please could you look again at your code and see if you can produce a working version?
Very many thanks,
Phil.

April 21, 2023 at 8:20 pm #13519819

Phil Cox

This thread is continued in a new thread at https://wpml.org/forums/topic/wpml-reports-languages-not-writeable/.