How to Efficiently Delete and Redirect Thousands of Tags in WordPress for SEO

One of the first tasks in SEO projects is to optimize the crawl budget and eliminate unnecessary low-quality pages.

Deleting redundant taxonomy, tags, and categories in WordPress is quite easy if the number is small. However, deleting hundreds or thousands of tags and setting up their redirections manually in a proper format can be a task that takes weeks.

We use HTTP status codes like 301 and 410 for deleted pages. We manage these with redirection plugins available on WordPress. In this article, I will base my information on the plugin I use: https://wordpress.org/plugins/redirection/.

To carry out these tasks, you need to have at least some knowledge of database management

First Step: Compiling the Entire Tag List

If you have access to the WordPress panel, you can easily obtain a list of all the tags. All you need to do is run a simple code and get the URL list in preview mode without publishing the page.

Alternatively, you can use tools like Screaming SEO Frog. However, you might miss some tags with this method.

Even if there’s a tag sitemap, tags with characters like -([+%^ may not be included in the sitemap.

Another alternative is to retrieve the tag list using plugins like sitemap, etc. An example plugin: https://wordpress.org/plugins/simple-sitemap/

The most reliable method is to create a list according to your preferences using your own code.

You can access your WordPress files through a plugin like https://wordpress.org/plugins/wp-file-manager/ or directly using connections like FTP, SSH.

It’s up to you.

<?php /* Template Name: Get All Tags */ ?>

<?php
$tags = get_terms( array(
'taxonomy' => 'post_tag',
'hide_empty' => false,
) );

if ( ! empty( $tags ) && ! is_wp_error( $tags ) ) {
foreach ( $tags as $tag ) {
echo esc_url( get_term_link( $tag ) ) . ';' . esc_html( $tag->name ) . '<br>';
}
}
?>

or you can check tag count with query.

<?php
$tags = get_terms( array(
'taxonomy' => 'post_tag',
'hide_empty' => false,
) );

if ( ! empty( $tags ) && ! is_wp_error( $tags ) ) {
foreach ( $tags as $tag ) {
echo esc_url( get_term_link( $tag ) ) . ';' . intval( $tag->count ) . '<br>';
}
}
?>

Create a file named ‘page-get-all-tags.php’ and place it in:
wp-content > themes > yourthemestheme > page-get-all-tags.php
Add your code inside this file within your theme.

After creating the file, go to the page section and choose ‘Add New Page’. For the page template, select ‘Get All Tags’. Do not publish the page. Once you preview it, you will see all the tags loaded. All your tags will appear in a format like this: https://tolgaaayric.com/tags/tagname;tagname. Copy all of them.

Second Step: Determine the Tags to be Deleted or Redirected

Copy all the tags into Google Sheets. In Sheets, select the column you’ve pasted into and then go to Data > Split text to column. Choose semicolon as the delimiter. After doing this, links will be in column A, and tag names will be in column B. You can perform any desired operations on the tags by filtering them. Here, you have two methods to proceed. The first method is to determine which ones will be deleted. The second method is to enter counterparts for the tags for which you’ll be doing a 301 redirection. I quickly create a list as follows.

Third Step: Getting Rid of Unnecessary Tags

After identifying the tags to be deleted, open a separate sheet. We need to format the tag names to be suitable for an SQL query. You will have URLs copied in the format of tolgaaayric.com/tags/sdfdsf. We need to convert all tags into the ‘sdfdsf’, format. First, let’s remove the domain name. Use the search and replace function to look for domain.com/tags/ and then end it with /. Next, replace it completely. What remains will be sdfdsf/. This format is still not suitable for the SQL query. Doing this for 10 tags is easy, but consider if there are 1000, we need to use a formula. In column B, paste

="'" & SUBSTITUTE(A1, "/", "") & "',"

and you’ll see that your tags are now in the format ‘sdfdsf’,

You can drag the formula down or copy it completely to replace all the data in column A.

After changing all the data, copy all the data in column B and perform a special paste into column A, selecting ‘only data’.

Note: If you assign a 301 to a current tag and don’t delete it, it may continue to exist in the sitemap or other parts of the site. That’s why you need to delete it. :)

DELETE t, tt
FROM wp_terms t
JOIN wp_term_taxonomy tt ON t.term_id = tt.term_id
WHERE tt.taxonomy = 'post_tag'
AND t.slug IN (
'tag1',
'tag2',
'tag3',
'tag4',
'tag5',
'tag6'
);

Please don’t forget to get backup.
You need to execute the SQL code mentioned above. You can use tools like PHPMYADMIN or Adminer. If you don’t have access to the hosting, you can quickly access the database in WordPress using a tool like https://wordpress.org/plugins/pexlechris-adminer/.

Copy the tags into the code, and it’s essential to remember to remove the comma in the last line where it reads ‘your-tagname’,. Otherwise, SQL will give an error.

When you run this code, all the tags you’ve specified will be deleted from WordPress.

Fourth Step: Properly Implementing HTTP Status Codes

Because Google will encounter many 404 errors on the site, we need to inform Google that these pages have been deleted. That’s why we’ll use the 410 status code. 410 means the page was once present, but it’s now deleted.

I’ll explain this based on the Redirection plugin. I’ve tried many plugins. Both the Redirection plugin and other plugins did not support importing the 410 code. Here, we will solve this problem by importing into the database via SQL.

Bulk 410 Operations – Redirection Plugin

First, open your database management tool. Find the ‘wp_redirection_items’ table from here. We’ll import a CSV file into this table.

Now, let’s prepare our CSV file.

url match_url match_data regex position last_count last_access group_id status action_type action_code action_data match_type title

These will be the headers of our CSV file.
Example – Click for bigger.

You will structure your CSV file as shown in the example.

There’s no trailing slash at the end of the match_url. Pay attention to this, or it won’t work.
Apart from the URL, structure the other data as shown in the example and copy it for the subsequent rows. You can use

=IF(RIGHT(A2,1)="/", LEFT(A2,LEN(A2)-1), A2)

formula as you did.

After completing your work, download the file as a CSV and go to your database management tool. In the example, I used Adminer. After selecting the ‘wp_redirection_items’ table, choose the import option located at the bottom.

After importing your CSV file, you can test it by going to the redirection management plugin panel.

Implementing the 301 Redirects

This is the easiest task. If you want to set up 301 redirects for the tags you’ve deleted, you need to create a CSV file.

Copy the 301 redirects from the file you created earlier for tag management to a new sheet. In column A, you’ll have the URL of the deleted tag, and in column B, you’ll have the new link. There won’t be any headers in the sheet. After finalizing the file, download it in CSV format.

Go to the import section of the Redirection plugin and upload it.

Now your redirects are ready.

Fifth Step: Perform Checks

Using tools like Screaming Frog SEO, you can scan the initial URL list you created to see which ones return 404, 200, 301, and 410 status codes. You can test the operations in this manner.

Leave a Reply