☰
WORDPRESS / PRACTICE

How to Safely Clean Up Expired Transients in WordPress

How to Safely Clean Up Expired Transients in WordPress

Transients are temporary values that WordPress and plugins use to cache API responses, query results, integration data, and other calculations. They have an expiration time, but the record does not necessarily disappear from the database immediately after it expires.

If many such records accumulate, the options table becomes larger and its maintenance less efficient. Below is a safe way to check the number of transient records, delete only those that have expired, and configure regular maintenance without clearing the entire cache.

What Are Transients and Why Do They Remain in the Database?

A transient is a temporary value with a key and an expiration time. In the standard setup, WordPress stores this data in the options table: the value itself and its expiration time are stored separately. Regular transients use the `_transient_` and `_transient_timeout_` prefixes.

After a transient expires, its record may remain in the database until WordPress or WP-CLI performs cleanup. Some expired values are deleted when the specific key is requested again, but not every record is necessarily checked immediately.

On sites with many plugins, API integrations, or frequent imports, this can gradually increase the size of the options table. However, the mere presence of transient records does not indicate a problem; their quantity, creation frequency, and size are what matter.

  • Transients are not intended for storing critical data.
  • Deleting a transient usually causes the plugin to recreate it during the next request.
  • Large or numerous records can affect backups and database maintenance.

How to Check the Number of Transient Keys

Before cleaning up, connect to the correct WordPress installation through WP-CLI and check the table prefix. Do not automatically assume it is `wp_`: a production site may use a different value.

For an approximate count of records in the options table, run the SQL query below. It counts all names that begin with `_transient_`, including expiration-time records.

This count does not show how many values have already expired. It serves as a baseline for comparison before and after maintenance.

  • Make sure the command is being run from the correct site's directory.
  • Verify the site URL through WP-CLI, especially if the server hosts multiple projects.
  • For more accurate analysis, remember that one transient is often represented by a pair of records: the value and the timeout.
wp db query "SELECT COUNT(*) AS transient_rows FROM wp_options WHERE option_name LIKE '_transient_%';"

Checking the Environment Before Running the Command

Before cleanup, verify which site and database the current WP-CLI instance will process. This is especially important on staging servers, in containers, and on multisite installations.

  • Run `wp option get home` to verify the site address.
  • Run `wp db prefix` to retrieve the current table prefix.
  • When necessary, use the `--url=example.com` parameter to select a specific site in the network.
wp option get home
wp db prefix
wp --info

Safely Delete Expired Transients with WP-CLI

For planned cleanup, use the standard WP-CLI command that deletes only records whose expiration time has passed. It is safer than manually deleting SQL rows because you do not have to match each transient value with its corresponding timeout record yourself.

Before running the command in production, create a database backup and make sure you have access to the command's execution log. Cleanup itself normally does not delete content, users, or plugin settings, but a backup is still a standard precaution.

  • Run the command with the user account and permissions used for WordPress administration.
  • After execution, repeat the record count and compare the result.
  • If the site is under load, perform the operation during a period of lower activity.
wp db export backup-before-transient-cleanup.sql
wp transient delete --expired
wp db query "SELECT COUNT(*) AS transient_rows FROM wp_options WHERE option_name LIKE '_transient_%';"

Why You Should Not Use --all Without a Specific Reason

The command with the `--all` parameter deletes all Transients, not only those that have expired. This may be appropriate for specific troubleshooting or after changing an integration's configuration, but it is not the best approach for regular maintenance.

After a complete cleanup, plugins will begin rebuilding their caches. If Transients contain responses from external APIs, the site may send many repeated requests at the same time. This can temporarily increase server load, exceed API limits, or make the first requests after cleanup slower.

It is also important to remember that a transient may contain more than page cache data. It can store the results of expensive queries or prepared integration data. Deleting it does not break the plugin itself, but it resets its temporary state.

  • For regular maintenance, delete only expired records.
  • Coordinate a complete cleanup with the developer or the documentation for the specific plugin.
  • After mass deletion, monitor PHP logs, external requests, and database load.

Multisite: Site Transients and Table Prefixes

In WordPress Multisite, regular Transients belong to a specific site in the network, while network or site transients are stored separately at the network level. Therefore, cleaning one site does not necessarily process network-level records.

Before the operation, determine whether you need to work with one site, the entire network, or only local records. You can select a site by passing its domain or path through `--url`. For network Transients, use the supported WP-CLI `--network` parameter, checking the documentation for the installed WP-CLI version.

Do not build SQL queries based on assumptions about table names. In a network, prefixes may differ between the main table and individual site tables, and network data is stored in a different table from local options data.

  • First, verify the required site with `wp --url=example.com option get home`.
  • Account separately for network Transients if they are used by plugins or the theme.
  • Do not run cleanup across the entire network if the task concerns only one site.
wp --url=example.com transient delete --expired
wp transient delete --expired --network

How to Configure Regular Cleanup with Cron

For scheduled maintenance, you can use system cron to run WP-CLI from the required directory. This reduces dependence on site visits and WP-Cron, especially when the site has irregular traffic.

Choose the interval according to how quickly data accumulates and the server's workload. There is no need to run cleanup too frequently if the number of records grows slowly. It is best to redirect the command output to a log or a separate file so that errors do not go unnoticed.

Before automating the process, verify which system user runs cron, whether PHP and WP-CLI are available, and whether the path to WordPress is correct.

  • Use an absolute path to `wp` and the site directory.
  • Do not suppress all errors without logging them.
  • Periodically check the log and the number of transient rows after execution.
  • As an alternative, review WordPress's built-in `delete_expired_transients` task if it works correctly in your configuration.
*/30 * * * * cd /var/www/example.com && /usr/local/bin/wp transient delete --expired >> /var/log/wp-transients.log 2>&1

Common Cleanup Mistakes

The most common mistake is manually deleting rows from `wp_options` with a single SQL query. This approach can easily leave related timeout records behind or accidentally affect data that does not belong to expired Transients.

Another problem is cleaning the wrong database or site. This can happen when a server hosts multiple WordPress projects, a nonstandard prefix is used, or a command is run without `--url` on a network installation.

You should also avoid treating a lower record count as the only sign of success. If a plugin immediately creates thousands of new Transients after cleanup, investigate its logic, the frequency of its cron tasks, or external API errors.

  • Do not manually delete all records matching the `_transient_%` pattern.
  • Do not run `--all` as a daily procedure without a clear reason.
  • Do not confuse Transients with permanent plugin settings.
  • If persistent object caching is used, check its behavior as well: some temporary data may not be stored in the options table.

Frequently asked questions

Is it safe to delete expired Transients on production?

Yes, provided the command is run in the correct environment, a backup has been created beforehand, and only records with expired lifetimes are deleted. After the procedure, check integrations and error logs.

Can all Transients be deleted with one command?

Technically, yes, with `wp transient delete --all`, but this should not be done regularly. A complete cleanup can cause plugins to repeat external requests and expensive calculations at the same time.

Why does the number of Transients quickly increase again after cleanup?

Plugins recreate temporary records as they operate. Rapid growth may be normal for an active integration, but it can also indicate an excessively short cache lifetime, API errors, or incorrect plugin logic.

Is it enough to delete records manually from the wp_options table?

No. Manual SQL can leave related expiration records behind or delete the wrong data. For standard Transients, use WP-CLI and reserve SQL for diagnostics and controlled analysis.

Conclusion

A safe strategy is not to delete all temporary data in bulk, but to regularly remove only records that have expired. Start by checking the site, table prefix, and backup, then use `wp transient delete --expired`.

For Multisite, account separately for local and network Transients. For automation, configure system cron with logging. If records accumulate too quickly again, investigate the specific plugin or integration instead of repeatedly performing a complete cleanup.

← All WordPress materials