Clean up wordpress database manually and safely

Last edited:
October 29, 2023
Reading time:
8 mins

/

Blog

/

Speed

/

Clean up wordpress databa...

đź’ˇWe may earn a commision if you subscribe to a service from a link on this page.

In the vast world of WordPress websites, ensuring a well-organized and streamlined database is vital for optimal performance and security. As your site grows and evolves, your database accumulates various types of data, ranging from legitimate content to redundant information and deprecated options. The continual growth of your database can lead to decreased site performance, longer backup times, and even potential security vulnerabilities. This guide delves into the importance of frequent database cleaning, situations that call for manual database clearance and optimizations, the process of navigating phpMyAdmin to carry out these tasks, and the crucial aspect of creating backups to safeguard your website throughout the cleaning process.

Why Frequent Database Cleaning is Necessary

WordPress databases serve as the backbone of your website, storing critical data such as posts, pages, settings, user information, and more. Over time, this repository of information can become cluttered, affecting your website in various ways.

Performance Degradation: With a cluttered database, the response times of your website can suffer, leading to a sluggish user experience. This not only frustrates your visitors but can negatively impact your site’s search engine rankings.

Increased Backup Size: As your database grows, so does the size of your backups. This requires more storage space, longer backup times, and can make the overall backup process unwieldy.

Security Risks: An unoptimized database can pose security risks, potentially exposing sensitive information and vulnerabilities to malicious actors.

Plugin and Theme Leftovers: When you uninstall plugins or themes, they may not always clean up after themselves properly, leaving behind remnants in your database that need manual removal.

Scenarios Requiring Manual Database Cleanup

Automated database cleanup plugins can be incredibly convenient, but there are specific situations where manual intervention becomes a necessity to ensure a comprehensive cleaning process. Over time, your database may accumulate fragments of old features that continue to linger, despite your best efforts. This could include data left behind by plugins that don’t completely remove their records, or remnants of old plugins and themes that have been updated but still retain outdated settings. In such cases, manual database cleaning is the most effective way to remove these remnants and optimize your WordPress site’s performance.

Uninstalling Plugins and Themes: Some plugins and themes might not perform comprehensive clean-up when uninstalled. Leftover database tables, settings, or data remnants may linger, requiring manual removal.

Overgrown Options Table: The wp_options table acts as a storage hub for various settings related to your site. Over time, it can accumulate expired or outdated data, which necessitates cleaning.

Post Revisions: WordPress saves post revisions automatically. Over time, these revisions can accumulate, leading to a cluttered database. Manual removal is often required to manage these effectively.

Unused Tables: Websites evolve, and with time, you may find yourself with tables in your database that are no longer relevant. These tables can accumulate from old plugins, themes, or experiments. Manual removal is the primary solution in such cases.

Navigating Through phpMyAdmin

But how exactly can we edit our database? Fortunately, there is only way to do that, and that is phpMyAdmin. There are a few implementations out there that require other tools, but that is out of our scope for this article. PHPmyAdmin is a web-based tool which simplifies the management of MySQL databases. Using it for database maintenance is highly effective.

Accessing phpMyAdmin: You can access phpMyAdmin through your hosting control panel, by adding “/phpmyadmin” to your website’s URL, or by using FTP/SSH to access your server directly.

Selecting the Database: Upon gaining access to phpMyAdmin, you select your WordPress database from the left-hand panel. This reveals a list of tables, each representing different aspects of your WordPress site.

Browsing Tables: To examine or modify a table, click on its name. This allows you to inspect the contents, make changes, and perform SQL queries for advanced database management.

Running Queries: If you’re comfortable with SQL, you can run queries to clean or optimize tables. Be cautious when using this method and always ensure you have backups to restore in case of any unforeseen issues. We’ll provide some detailed instructions below to play around safely.

Backing Up the Database (and the Entire Website)

Before engaging in any manual database cleaning, it is imperative to create backups to ensure the safety of your website and data.

Backup Database: In phpMyAdmin, use the “Export” tab to select the tables you wish to back up. Choose your preferred export method, either SQL or custom, and download the SQL file to your local machine.

Backup Entire Website: Since you are already in your hosting panel, and your database is already backed up, take a minute to back your website files also. By keeping a local compressed archive of your site files, databases, and other essential data, you can guarantee complete website protection. After all, we are going to make some changes and it’s safer for you to have a complete backup of the website. Just visit your File Manager, locate public_html folder, compress it and download it.

Cleaning WordPress Database Manually

Deleting Tables from Unused Plugins and Themes: While WordPress is user-friendly, it doesn’t always clean up after you when you uninstall plugins and themes. Over time, this can lead to the accumulation of unused database tables that should be manually deleted.

For example, we have an installation where we installed Rank Math to replace Yoast SEO. Upon deleting Yoast SEO, some tables have remained undeleted. First, let’s visit PHPmyadmin and select our database. While browsing our tables, we can discover some old Yoast SEO related tables which we can safely remove:

Deleting Autoload Options: Autoload options in the wp_options table are specifically marked to load automatically with each page. While these options are designed to enhance site speed, over time, they may do the exact opposite and contribute to slower database performance. Deleting autoload options that are no longer necessary can significantly boost your site’s speed. Just like with the wp_options table, exercise caution and ensure you identify and keep any autoload options that are essential for your WordPress site’s proper functioning.

In order to delete autoload options, visit the SQL tab after selecting your database:

Inside the textarea field, add the below SQL code to get the top 20 “heavier” autoloading options:

SELECT option_name, length(option_value) AS option_value_length FROM wp_options WHERE autoload='yes' ORDER BY option_value_length DESC LIMIT 20;

You can now discover which options are autoloading in every request and consume the most resources. A few traces of Yoast SEO options are still left to delete, as long as some other deprecated settings:

Clearing the wp_options Table: The wp_options table is a hub for various settings related to your WordPress site. It accumulates data from active and inactive plugins and themes. Identifying and removing expired or unused entries is critical for optimal site performance. However, exercise caution when doing this, and be sure you are deleting currently used options to avoid issues.

In order to find the identify the biggest options of your wp_options table, use the same SQL code as before but remove the autoload=”yes” part:

SELECT option_name, length(option_value) AS option_value_length FROM wp_options WHERE autoload='yes' ORDER BY option_value_length DESC LIMIT 20;

Clearing Post Revisions: WordPress automatically stores revisions of your posts and pages. While this can be valuable for editing history, it can also lead to a cluttered database. Using SQL queries, you can efficiently remove outdated post revisions. To prevent future accumulation, consider setting limits on the number of revisions stored. To list all revisions (in case you want to keep the latest and delete only the older ones), use the below SQL code:

SELECT ID, post_title, post_date, post_type
FROM wp_posts
WHERE post_type = 'revision'
ORDER BY post_date DESC
LIMIT 20;

Delete Transients: Transients are temporary data stored in your database. They often serve as caches or short-term storage. Over time, these transients can accumulate and affect your database’s efficiency. Cleaning them periodically is a wise practice to maintain database health. If you want to get rid of all the transients, you can use the below SQL code to list them (and delete whichever you want):

SELECT option_name, length(option_value) AS option_value_length
FROM wp_options
WHERE option_name LIKE '_transient_%' OR option_name LIKE '_site_transient_%'
ORDER BY option_value_length DESC
LIMIT 20;

Verifying the Success of Cleanup

Database Size: Comparing your database size before and after cleaning is a straightforward method to verify the success of your cleanup efforts. A noticeable reduction in size indicates that the process was effective.

Website Performance: Test your website’s performance by navigating through it after the cleanup. Faster loading times, smoother interactions, and improved site speed are indicators of a successful cleanup.

Error Checks: After any cleanup operation, monitor your site closely for errors or unusual behavior. It’s crucial to be prepared to restore from your backup in case any unforeseen issues arise.

Optimize your WordPress website database!

Maintaining a clean and efficient database is essential for the continued performance and security of your WordPress website. Frequent database cleaning, especially in scenarios where manual clearance and optimizations are required, can lead to a faster, more secure, and more reliable website. By using phpMyAdmin and following best practices, you can ensure your database remains in top shape, ultimately benefiting both you and your site’s visitors.

Leave a Reply

Your email address will not be published. Required fields are marked *