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 4 replies, has 2 voices.

Last updated by johnnyA 2 years, 2 months ago.

Assisted by: Nigel.

Author Posts
August 24, 2022 at 9:35 pm #11925277

johnnyA

I am trying to: We stopped trying to implement WMPL because we found it to be way too resource intensive, but now we've realized using it has duplicated entries for all the images in the media library. Our client has about 4,000 images, so it results in ~12,000 image entries when we disable or delete the plugin. The only information on the support forum says i'll have to bulk delete (ONLY 80 items at at time?) for each language manually from the backend of the site. I'll have to hand delete 8,000 image entries from the backend of WordPress?

This is unacceptable. When deleting a plugin, all the associated data should go with it.

Link to a page where the issue can be seen: eversana.com/wp-admin/upload.php

I expected to see: one image for each media entry

Instead, I got: three to eight of the same image, for each image entry

Will this be fixed in the near future? This is truly awful.

Screen Shot 2022-08-24 at 4.30.53 PM.png
August 25, 2022 at 7:28 am #11926773

Nigel
Supporter

Timezone: Europe/Madrid (GMT+01:00)

Hi there

Are the WPML database tables still present?

If you delete the WPML plugin it doesn't automatically delete its tables (there can be a variety of reasons for deleting the plugin where you would not want to delete the tables). There is a method to delete them described in this FAQ: https://wpml.org/faq/how-wpml-reset-works/

But if you want to want to delete multilingual content (e.g. media posts) programmatically rather than manually it is important you do so while the WPML tables are still intact, so that the duplicated content can be identified.

If your WPML tables are still intact then I can prepare a script you can use to bulk delete the translated content.

August 25, 2022 at 2:42 pm #11930087

johnnyA

I tested deleting the plugin from a local version of the site, not the production version.

I don't know how to tell if WPML database tables are present.

Yes, I do want to delete these duplicate media entries. Once you turn off the plugin, it makes the media library extremely hard for our client to use, because they all the duplicates show up at once.

August 26, 2022 at 10:27 am #11936463

Nigel
Supporter

Timezone: Europe/Madrid (GMT+01:00)

OK, if you haven't taken explicit steps to delete the WPML database tables they will still be there.

So I have prepared a sample script you can use to delete the translated media posts, as well as deleting translated posts that would still be left in the wp_posts table (and associated meta data), as well as removing translated categories and tags from the relevant taxonomy tables.

You would run this script once. Before doing so you really should make sure you have a backup so that if something unexpected occurs you can restore the backup. (I have tested the script on my own site, but cannot rule out that something unexpected could happen on yours.)

Once you have run the script, which will have removed the translated posts etc., you can then complete the process of removing WPML (which will delete all of its tables) by following the steps I linked to in the FAQ earlier (https://wpml.org/faq/how-wpml-reset-works/).

To run the script I suggest you use the plugin Code Snippets, where you can add custom PHP, and which has the option to execute the script one time only.

This is the script itself:

/**
 * Run snippet one-time to delete translated CONTENT (posts, media, taxonomies) when removing WPML from a site
 * 
 * You MUST have an up-to-date backup before running this code in case of unexpected results
 */

global $wpdb;

/* Delete translated media posts */
// Get IDs of all translated media
$results = $wpdb->get_results( "SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE element_type = 'post_attachment' AND source_language_code IS NOT null", OBJECT );
foreach ($results as $result){
	// delete the attachment post but not the media file
	// cannot use wp_delete_attachment because it deletes the media file
	// cannot use wp_delete_post because for attachments it switches to wp_delete_attachment
	// solution is to first switch post type to something else then use wp_delete_post
	// use direct query to avoid overhead of wp_update_post
	
	$wpdb->update(
		"{$wpdb->prefix}posts",
		array( 'post_type' => 'for-deletion' ),
		array( 'ID' => $result->element_id ),
		array( '%s' ),
		array( '%d' )
	);
	// delete safely using wp_delete_post
	wp_delete_post( $result->element_id, true );
}

/* Delete translated posts and pages */
// Get IDs of all translated posts
$results = $wpdb->get_results( "SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE element_type IN ('post_post','post_page') AND source_language_code IS NOT null", OBJECT );
foreach ($results as $result){
	wp_delete_post( $result->element_id, true );
}


/** Taxonomies 
 * First delete the WPML taxonomy_priority terms
 * 
 * Then delete all translated terms
 */

// Delete translation_priority terms
$terms = get_terms( array(
		'taxonomy' => 'translation_priority',
		'hide_empty' => false
	) 
);
foreach ( $terms as $term ) {
	wp_delete_term($term->term_id, 'translation_priority'); 
}

// Delete all translated categories and tags
// Get IDs of all translated categories and tags
$results = $wpdb->get_results( "SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE element_type IN ('tax_category','tax_post_tag') AND source_language_code IS NOT null", OBJECT );
// Get an array of term ids along with their taxonomy, needed for wp_delete_term
$terms = $wpdb->get_results( "SELECT term_id,taxonomy FROM wp_term_taxonomy", OBJECT_K );
foreach ($results as $result){
	$term_id = $result->element_id;
	$taxonomy = $terms[$term_id]->taxonomy;
	wp_delete_term( $term_id, $taxonomy );	
}

Once you have completed these steps you should only see the original language version of the media in the Media Library.

Let me know how you get on.

August 30, 2022 at 11:26 pm #11960479

johnnyA

Thanks Nigel. Your script worked well. I tried it first on a development version of the site. The code snippet plugin trick worked great too. Thanks a ton!