class-database-cleanup.php

Database Cleanup functionality.

Source includes/Database/class-database-cleanup.php16 min readPart of Performance Optimisation

includes/Database/class-database-cleanup.php

Database Cleanup functionality.

Namespace: PerformanceOptimise\\Inc · Lines: 3365

Class Database_Cleanup

Class Database_Cleanup

Source: includes/Database/class-database-cleanup.php, line 31

class Database_Cleanup

Tags: @since 1.1.0

Constants

ConstantVisibilityValueLine
TABLE_MAPpublicarray( \'revisions\' => array( \'posts\', \'postmeta\' ), \'auto_drafts\' => array( \'posts\', \'postmeta\' ), \'trashed_posts\' => array( \'posts\', \'postmeta\' ), \'spam_comments\' => array( \'comments\', \'commentmeta\' ), \'trashed_comments\' => array( \'comments\', \'commentmeta\' ), \'expired_transients\' => array( \'options\' ), \'orphan_postmeta\' => array( \'postmeta\' ), \'unattached_media\' => array( \'posts\', \'postmeta\' ), \'oembed_cache\' => array( \'options\' ), )42
MONTH_SECONDSprivate267840064
FAILED_PURGE_MAX_LIFESPANprivate3 * self::MONTH_SECONDS80
FAILED_PURGE_BATCH_MAXprivate10092
FAILED_PURGE_MAX_ITERATIONSprivate10100
FAILED_PURGE_BUDGET_SECONDSprivate15.0109
FAILED_PURGE_RESERVE_SECONDSprivate2.0119
METHOD_TO_TYPEprivatearray( \'clean_revisions_advanced\' => \'revisions\', \'clean_auto_drafts\' => \'auto_drafts\', \'clean_trashed_posts\' => \'trashed_posts\', \'clean_spam_comments\' => \'spam_comments\', \'clean_trashed_comments\' => \'trashed_comments\', \'clean_expired_transients\' => \'expired_transients\', \'clean_orphan_postmeta\' => \'orphan_postmeta\', \'clean_unattached_media\' => \'unattached_media\', \'clean_oembed_cache\' => \'oembed_cache\', )143
CLEANUP_METHOD_MAPpublicarray( \'revisions\' => \'clean_revisions_advanced\', \'auto_drafts\' => \'clean_auto_drafts\', \'trashed_posts\' => \'clean_trashed_posts\', \'spam_comments\' => \'clean_spam_comments\', \'trashed_comments\' => \'clean_trashed_comments\', \'expired_transients\' => \'clean_expired_transients\', \'orphan_postmeta\' => \'clean_orphan_postmeta\', \'unattached_media\' => \'clean_unattached_media\', \'oembed_cache\' => \'clean_oembed_cache\', )164
ACTION_SCHEDULER_TYPEpublic\'action_scheduler\'196
SALT_KEYprivate\'wppo_db_cleanup_salt\'217
REMEDIATED_OPTIONpublic\'wppo_autoload_remediated\'980
AUTOLOAD_SIZE_THRESHOLDpublic1024988
AUTOLOAD_CRITICAL_BYTESpublic8192001003
AUTOLOAD_REMEDIATION_LIMITpublic1001011
AUTOLOAD_THRESHOLD_MINpublic1001022
AUTOLOAD_THRESHOLD_MAXpublic104857601030
AUTOLOAD_LIMIT_MAXpublic5001038
EXPORT_LIMIT_MAXpublic20001046
AUTOLOAD_SIZE_LIMIT_DEFAULTpublic8000001450

Properties

PropertyVisibilityTypeDefaultLine
$health_memoprivate staticarray|nullnull127
$health_memo_timeprivate staticfloat0.0135
$defer_counts_invalidationprivate staticboolfalse3085

publicstatic get_cleanup_method_map()

public static function get_cleanup_method_map(): array

Get the cleanup method map (type => method).

Return: array<string, — string>.

Tags: @since 2.0.0

Source: includes/Database/class-database-cleanup.php, line 182

publicstatic get_valid_cleanup_types()

public static function get_valid_cleanup_types(): array

Get valid cleanup types including \’all\’.

Return: string[].

Tags: @since 2.0.0

Source: includes/Database/class-database-cleanup.php, line 207

privatestatic delete_in_batches()

private static function delete_in_batches(string $select_sql, string $meta_table, string $meta_column, string $main_table, string $id_column, int=1000 $batch): int|false

Batched DELETE helper for post/comment cleanup.

ParameterTypeDefaultDescription
$select_sqlstring—SQL returning a single ID column (must include LIMIT).
$meta_tablestring—Fully-qualified meta table name (e.g. $wpdb->postmeta).
$meta_columnstring—FK column in the meta table (e.g. post_id).
$main_tablestring—Fully-qualified main table name (e.g. $wpdb->posts).
$id_columnstring—PK column in the main table (e.g. ID).
$batchint=1000—Batch size; matches the LIMIT in $select_sql.

Return: int|false — Number of main rows deleted, or false on SQL error.

Tags: @since 2.0.0

Source: includes/Database/class-database-cleanup.php, line 244

publicstatic clean_revisions()

public static function clean_revisions(): int|false

Delete all post revisions from the database.

Return: int|false — The number of rows deleted, or `false` on SQL error.

Tags: @since 1.1.0

Source: includes/Database/class-database-cleanup.php, line 347

publicstatic clean_revisions_advanced()

public static function clean_revisions_advanced(=30 $max_age_days, =5 $keep_latest)

Remove post revision records older than a computed cutoff while keeping the latest revisions per parent post.

ParameterTypeDefaultDescription
$max_age_days=30—Maximum age in days (clamped to 1-365; audit #1362); revisions older than now – $max_age_days will be eligible for deletion.
$keep_latest=5—Number of most recent revisions to retain per parent post.

Return: int|false — Number of rows deleted, or `false` on database error.

Tags: @since 1.3.0

Source: includes/Database/class-database-cleanup.php, line 368

privatestatic flush_revision_deletes()

private static function flush_revision_deletes(array $ids, int& $deleted): bool

Flush a batch of revision IDs to the database (postmeta + posts) in 50-row chunks.

ParameterTypeDefaultDescription
$idsarray—Revision IDs to delete.
$deletedint&—Running deleted counter (passed by reference, incremented).

Return: bool — True on success, false on SQL error.

Tags: @since 2.0.0

Source: includes/Database/class-database-cleanup.php, line 503

publicstatic clean_auto_drafts()

public static function clean_auto_drafts()

Remove all auto-draft posts and their associated postmeta in batched operations.

Return: int|false — Total number of posts deleted, or `false` on SQL error.

Tags: @since 1.1.0

Source: includes/Database/class-database-cleanup.php, line 544

publicstatic clean_trashed_posts()

public static function clean_trashed_posts()

Remove all posts with status \’trash\’ and their associated postmeta.

Return: int|false — Total number of posts deleted, or `false` on SQL error.

Tags: @since 1.1.0

Source: includes/Database/class-database-cleanup.php, line 563

publicstatic clean_spam_comments()

public static function clean_spam_comments()

Delete all spam comments.

Return: int|false — Number of rows deleted, or false on error.

Tags: @since 1.1.0 · @since 2.0.0 Exclude WP 6.9+ Notes (`comment_type=\’note\’`).

Source: includes/Database/class-database-cleanup.php, line 588

publicstatic clean_trashed_comments()

public static function clean_trashed_comments()

Remove trashed comments and their comment meta from the database in batches.

Return: int|false — Number of rows deleted, or false on error.

Tags: @since 1.1.0 · @since 2.0.0 Exclude WP 6.9+ Notes (`comment_type=\’note\’`).

Source: includes/Database/class-database-cleanup.php, line 613

publicstatic clean_expired_transients()

public static function clean_expired_transients()

Delete expired transients and their timeout entries from the options table.

Return: int|false — `int` number of option rows deleted, `false` on SQL error.

Tags: @since 1.1.0

Source: includes/Database/class-database-cleanup.php, line 634

publicstatic clean_orphan_postmeta()

public static function clean_orphan_postmeta()

Delete orphaned post meta.

Return: int|false — Number of rows deleted, or false on error.

Tags: @since 1.1.0

Source: includes/Database/class-database-cleanup.php, line 726

publicstatic clean_unattached_media()

public static function clean_unattached_media()

Delete media attachments with no parent post.

Return: int|false — Number of attachments deleted, or false on error.

Tags: @since 2.0.0

Source: includes/Database/class-database-cleanup.php, line 783

publicstatic get_autoloadable_values()

public static function get_autoloadable_values(): array

Autoload values that count as \”autoloaded\” for the options audit.

Return: string[].

Tags: @since 2.0.0

Source: includes/Database/class-database-cleanup.php, line 846

publicstatic is_wp_version_at_least()

public static function is_wp_version_at_least(string $version): bool

Whether the running WordPress version is at least the given version.

ParameterTypeDefaultDescription
$versionstring—Minimum version (e.g. \’6.6\’).

Return: bool.

Tags: @since 2.2.0

Source: includes/Database/class-database-cleanup.php, line 882

publicstatic get_default_autoload_value()

public static function get_default_autoload_value(): string

Default autoload value for newly created options.

Return: string.

Tags: @since 2.2.0

Source: includes/Database/class-database-cleanup.php, line 908

publicstatic get_autoloaded_options()

public static function get_autoloaded_options(int=20 $limit): array

List the largest autoloaded options, by stored byte size.

ParameterTypeDefaultDescription
$limitint=20—Maximum number of options to return.

Return: array<int, — array{option_name:string,size:int,autoload:string}> Sorted by size.

Tags: @since 2.0.0

Source: includes/Database/class-database-cleanup.php, line 932

publicstatic get_core_autoload_options()

public static function get_core_autoload_options(): array

Core option names that must never have their autoload value flipped.

Return: string[].

Tags: @since 2.0.0

Source: includes/Database/class-database-cleanup.php, line 1056

publicstatic is_autoload_remediation_supported()

public static function is_autoload_remediation_supported(): bool

Whether the runtime supports flipping autoload values (WP 6.6+).

Return: bool.

Tags: @since 2.0.0

Source: includes/Database/class-database-cleanup.php, line 1162

publicstatic get_autoload_remediation_threshold()

public static function get_autoload_remediation_threshold(=null $settings): int

Resolve the remediation size threshold from settings with bounds.

ParameterTypeDefaultDescription
$settings=null—Optional settings array or null to load from option.

Return: int — Threshold in bytes.

Tags: @since 2.0.0

Source: includes/Database/class-database-cleanup.php, line 1189

publicstatic get_autoload_total_bytes()

public static function get_autoload_total_bytes(): int

Get the total bytes currently held by autoloaded options.

Return: int — Total bytes, or 0 on error.

Tags: @since 2.0.0

Source: includes/Database/class-database-cleanup.php, line 1210

publicstatic get_autoload_count()

public static function get_autoload_count(): int

Get the number of currently autoloaded options.

Return: int — Count, or 0 on error.

Tags: @since 2.2.0

Source: includes/Database/class-database-cleanup.php, line 1242

publicstatic get_autoload_critical_threshold()

public static function get_autoload_critical_threshold(): int

Resolve the critical autoload threshold in bytes.

Return: int — Threshold in bytes (> 0).

Tags: @since 2.3.0

Source: includes/Database/class-database-cleanup.php, line 1276

publicstatic is_autoload_critical()

public static function is_autoload_critical(?int=null $total, ?int=null $threshold): bool

Whether the autoload payload is at or above the critical threshold.

ParameterTypeDefaultDescription
$total?int=null—Optional total bytes (defaults to {@see get_autoload_total_bytes()}).
$threshold?int=null—Optional threshold bytes (defaults to {@see get_autoload_critical_threshold()}; pass the already-resolved value to avoid a second filter call).

Return: bool — True when $total >= critical threshold.

Tags: @since 2.3.0

Source: includes/Database/class-database-cleanup.php, line 1307

publicstatic get_autoload_audit()

public static function get_autoload_audit(int=20 $limit): array

Single-call autoload audit payload aligned to WP 6.6 semantics.

ParameterTypeDefaultDescription
$limitint=20—Maximum number of options to return.

Return: array{total_autoload_bytes:int,count:int,critical_threshold:int,is_critical:bool,options:array}.

Tags: @since 2.3.0

Source: includes/Database/class-database-cleanup.php, line 1334

publicstatic get_autoload_parity()

public static function get_autoload_parity(): array

Compare the reported total against wp_load_alloptions() size.

Return: array{reported:int,alloptions_bytes:int,delta:int,matches_within_rounding:bool}.

Tags: @since 2.3.0

Source: includes/Database/class-database-cleanup.php, line 1399

publicstatic get_autoload_size_limit()

public static function get_autoload_size_limit(): int

Get the Site Health autoloaded-options size limit in bytes.

Return: int — Size limit in bytes.

Tags: @since 2.3.0

Source: includes/Database/class-database-cleanup.php, line 1462

publicstatic get_autoload_candidates()

public static function get_autoload_candidates(int=self::AUTOLOAD_SIZE_THRESHOLD $threshold, int=self::AUTOLOAD_REMEDIATION_LIMIT $limit): array

List non-core autoloaded options at or above the size threshold.

ParameterTypeDefaultDescription
$thresholdint=self::AUTOLOAD_SIZE_THRESHOLD—Minimum option size in bytes.
$limitint=self::AUTOLOAD_REMEDIATION_LIMIT—Maximum number of candidates to return.

Return: array<int, — array{option_name:string,size:int,autoload:string}> Sorted by size desc.

Tags: @since 2.0.0

Source: includes/Database/class-database-cleanup.php, line 1487

privatestatic clamp_autoload_threshold()

private static function clamp_autoload_threshold(int $threshold): int

Clamp a caller-supplied remediation threshold to the supported range.

ParameterTypeDefaultDescription
$thresholdint—Threshold in bytes.

Return: int — Clamped threshold in bytes.

Tags: @since 2.0.0

Source: includes/Database/class-database-cleanup.php, line 1569

publicstatic plan_autoload_remediation()

public static function plan_autoload_remediation(?int=null $threshold, int=self::AUTOLOAD_REMEDIATION_LIMIT $limit): array

Build a dry-run remediation report without changing anything.

ParameterTypeDefaultDescription
$threshold?int=null—Optional threshold override (bytes).
$limitint=self::AUTOLOAD_REMEDIATION_LIMIT—Maximum number of candidates.

Return: array{threshold:int,supported:bool,total_autoload_bytes:int,count:int,bytes_saved:int,options:array,backup:array}.

Tags: @since 2.0.0

Source: includes/Database/class-database-cleanup.php, line 1585

publicstatic get_remediated_options()

public static function get_remediated_options(): array

Get stored prior autoload values for remediated options.

Return: array<string,string> — Map of option_name => prior autoload value.

Tags: @since 2.0.0

Source: includes/Database/class-database-cleanup.php, line 1617

privatestatic set_option_autoload_off()

private static function set_option_autoload_off(string $option_name, string=\'no\' $autoload_off): bool

Flip one option\’s autoload value off, guarded for WP 6.6+ with legacy fallback.

ParameterTypeDefaultDescription
$option_namestring—Option name.
$autoload_offstring=\'no\'—Value disabling autoload (\’off\’ on WP 6.6+, \’no\’ legacy).

Return: bool — True on success, false on failure.

Tags: @since 2.0.0

Source: includes/Database/class-database-cleanup.php, line 1634

privatestatic restore_option_autoload()

private static function restore_option_autoload(string $option_name, string $prior): bool

Restore one option\’s prior autoload value, guarded like the flip path.

ParameterTypeDefaultDescription
$option_namestring—Option name.
$priorstring—Prior autoload value to restore.

Return: bool — True on success, false on failure.

Tags: @since 2.0.0

Source: includes/Database/class-database-cleanup.php, line 1673

publicstatic remediate_autoload()

public static function remediate_autoload(?int=null $threshold, int=self::AUTOLOAD_REMEDIATION_LIMIT $limit): array

Apply one-click autoload remediation: flip candidates to autoload off, one at a time.

ParameterTypeDefaultDescription
$threshold?int=null—Optional threshold override (bytes).
$limitint=self::AUTOLOAD_REMEDIATION_LIMIT—Maximum number of options to flip.

Return: array{threshold:int,supported:bool,applied:array,failed:array,bytes_saved:int,total_autoload_bytes:int,backup:array}.

Tags: @since 2.0.0

Source: includes/Database/class-database-cleanup.php, line 1735

publicstatic revert_autoload_option()

public static function revert_autoload_option(string $option_name)

Revert a single remediated option to its prior autoload value.

ParameterTypeDefaultDescription
$option_namestring—Option name to revert.

Return: bool|WP_Error — True on success, WP_Error when unknown/failed.

Tags: @since 2.0.0

Source: includes/Database/class-database-cleanup.php, line 1826

publicstatic revert_autoload_all()

public static function revert_autoload_all(): array

Revert all remediated options to their prior autoload values.

Return: array{reverted:string[],failed:string[],restored:array,total_autoload_bytes:int}.

Tags: @since 2.0.0

Source: includes/Database/class-database-cleanup.php, line 1863

publicstatic export_expired_transients()

public static function export_expired_transients(int=500 $limit): array

Export expired transients for pre-run review (read-only, expired rows only).

ParameterTypeDefaultDescription
$limitint=500—Maximum number of rows to export.

Return: array<int, — array{option_name:string,timeout_option:string,expired_at:int,size:int}>.

Tags: @since 2.0.0

Source: includes/Database/class-database-cleanup.php, line 1911

publicstatic clean_oembed_cache()

public static function clean_oembed_cache()

Clean oEmbed cache.

Return: bool|int — Number of deleted options or false on error.

Source: includes/Database/class-database-cleanup.php, line 1991

publicstatic is_action_scheduler_available()

public static function is_action_scheduler_available(): bool

Whether the Action Scheduler store/cleaner API is available.

Return: bool — True when the store + cleaner classes exist.

Tags: @since 2.2.0

Source: includes/Database/class-database-cleanup.php, line 2046

publicstatic is_action_scheduler_store_available()

public static function is_action_scheduler_store_available(): bool

Whether the Action Scheduler store API alone is available.

Return: bool — True when the store class exists.

Tags: @since 2.2.0

Source: includes/Database/class-database-cleanup.php, line 2065

privatestatic get_action_scheduler_retention()

private static function get_action_scheduler_retention(): array

Resolve the Action Scheduler retention cutoffs using AS\’s own filters.

Return: array{lifespan:int,lifespan_failed:int}.

Tags: @since 2.2.0

Source: includes/Database/class-database-cleanup.php, line 2085

privatestatic get_action_scheduler_cutoff()

private static function get_action_scheduler_cutoff(int $seconds): ?\\DateTime

Build a cutoff DateTime N seconds ago via AS when possible.

ParameterTypeDefaultDescription
$secondsint—Age in seconds.

Return: \\DateTime|null — Null when no datetime API is available.

Tags: @since 2.2.0

Source: includes/Database/class-database-cleanup.php, line 2111

publicstatic get_action_scheduler_health()

public static function get_action_scheduler_health(): array

Queue health for the Action Scheduler tables (read-only).

Return: array{available:bool,pending:int,failed:int,oldest_pending_age_seconds:int|null,reclaimable:int,reclaimable_bytes:int,total_bytes:int}.

Tags: @since 2.2.0

Source: includes/Database/class-database-cleanup.php, line 2136

publicstatic reset_action_scheduler_health_cache()

public static function reset_action_scheduler_health_cache(): void

Reset the Action Scheduler health memo (test seam).

Return: void.

Tags: @since 2.2.0

Source: includes/Database/class-database-cleanup.php, line 2367

publicstatic is_failed_action_purge_enabled()

public static function is_failed_action_purge_enabled(): bool

Whether the opt-in failed-action purge is enabled.

Return: bool — True when failed actions older than 3 months may be purged.

Tags: @since 2.2.0

Source: includes/Database/class-database-cleanup.php, line 2394

publicstatic purge_failed_actions()

public static function purge_failed_actions(int=50 $batch_size, bool=true $log, ?float=null $deadline): int

Purge failed Action Scheduler actions older than 3 months in batches.

ParameterTypeDefaultDescription
$batch_sizeint=50—Maximum rows deleted per iteration. Default 50, clamped to 1-100.
$logbool=true—Whether to write the activity-log row and invalidate the counts cache. Pass false when a wrapper (e.g. clean_action_scheduler()) logs the combined total itself, so one purge emits one row and invalidates once (issue #1310 review).
$deadline?float=null—Optional shared wall-clock deadline (microtime(true) value) inherited from a wrapper that already spent part of its budget. Null starts a fresh 15s budget for standalone calls.

Return: int — Number of failed actions deleted.

Tags: @since 2.2.0

Source: includes/Database/class-database-cleanup.php, line 2450

publicstatic clean_action_scheduler()

public static function clean_action_scheduler(): int

Purge terminal Action Scheduler actions past AS retention.

Return: int — Number of actions deleted (0 when AS absent, disabled, or nothing past retention).

Tags: @since 2.2.0

Source: includes/Database/class-database-cleanup.php, line 2588

publicstatic clean_all()

public static function clean_all()

Execute all defined database cleanup routines and collect their results.

Return: array<string, — int|WP_Error> Associative array keyed by cleanup type (e.g. \’revisions\’, \’auto_drafts\’) with each value set to the number of rows deleted or a `WP_Error` instance if that cleanup failed.

Tags: @since 1.1.0

Source: includes/Database/class-database-cleanup.php, line 2668

publicstatic get_revision_defaults()

public static function get_revision_defaults(=null $settings)

Get revision cleanup defaults.

ParameterTypeDefaultDescription
$settings=null—Optional settings array or null to load from option.

Return: array{0:int,1:int} — Tuple of [max_age_days, keep_latest].

Tags: @since 2.0.0

Source: includes/Database/class-database-cleanup.php, line 2731

publicstatic auto_clean()

public static function auto_clean($settings)

Execute configured database cleanup routines according to provided settings.

ParameterTypeDefaultDescription
$settingsarray—Cleanup settings. Recognized keys: – \’dbRevMaxAge\’ (int) Maximum age in days for revision pruning (default 30). – \’dbRevKeepLatest\’ (int) Number of latest revisions to retain per parent (default 5).

Return: string[] — List of methods that failed.

Tags: @since 2.0.0

Source: includes/Database/class-database-cleanup.php, line 2759

publicstatic get_counts()

public static function get_counts()

Get current counts for each database cleanup category.

Return: array<string,int> — Associative array mapping cleanup type to its current count.

Tags: @since 1.1.0 · @since 2.0.0 Exclude WP 6.9+ Notes from spam/trashed comment counts. · @since 2.2.0 Add `action_scheduler` reclaimable count.

Source: includes/Database/class-database-cleanup.php, line 2838

publicstatic invoke_cleanup_method()

public static function invoke_cleanup_method($method, ... $args)

Call a static cleanup method by name and convert a `false` result into a `WP_Error`.

ParameterTypeDefaultDescription
$methodmixed—The static method name to invoke (string; other types are guarded).
$args...——

Return: mixed — The invoked method\’s return value, or a `WP_Error` if the method returned `false` or is not callable.

Tags: @since 1.4.0 · @since 2.0.0 Added method whitelist + is_callable guard returning WP_Error.

Source: includes/Database/class-database-cleanup.php, line 3042

publicstatic reset_runtime_state()

public static function reset_runtime_state(): void

Reset per-blog cleanup health and deferral memos.

Return: void.

Tags: @since NEXT

Source: includes/Database/class-database-cleanup.php, line 3093

publicstatic invalidate_counts_cache()

public static function invalidate_counts_cache(): void

Invalidate the DB cleanup counts cache by incrementing the salt or deleting the transient.

Return: void.

Tags: @since 2.0.0

Source: includes/Database/class-database-cleanup.php, line 3105

publicstatic on_post_change()

public static function on_post_change($post_id, ?\\WP_Post=null $post): void

Callback for save_post/deleted_post to invalidate DB cleanup counts for public post types.

ParameterTypeDefaultDescription
$post_idint—Post ID.
$post?\\WP_Post=null—Post object.

Return: void.

Tags: @since 2.0.0

Source: includes/Database/class-database-cleanup.php, line 3140

privatestatic get_tables_size()

private static function get_tables_size(array $tables): int

Get the combined size (data + index) of several database tables in bytes.

ParameterTypeDefaultDescription
$tablesarray—Full table names (including prefix).

Return: int — Combined size in bytes, or 0 if unknown.

Tags: @since 2.2.0

Source: includes/Database/class-database-cleanup.php, line 3164

privatestatic get_table_size()

private static function get_table_size(string $table): int

Get the size (data + index) of a database table in bytes.

ParameterTypeDefaultDescription
$tablestring—Full table name (including prefix).

Return: int — Table size in bytes, or 0 if unknown.

Tags: @since 2.0.0

Source: includes/Database/class-database-cleanup.php, line 3205

publicstatic optimize_table()

public static function optimize_table(string $table): bool

Run OPTIMIZE TABLE on a given table to reclaim disk space and rebuild indexes.

ParameterTypeDefaultDescription
$tablestring—Unprefixed table identifier (e.g. \’posts\’, \’postmeta\’).

Return: bool — True on success, false on invalid identifier, empty table, skipped due to size (>1GB), or query failure.

Tags: @since 2.0.0 Added allowlist justification and verified no user input reaches interpolation.

Source: includes/Database/class-database-cleanup.php, line 3258

publicstatic maybe_optimize_tables()

public static function maybe_optimize_tables(array $table_names, bool $enabled): void

Conditionally optimize a list of unique database tables.

ParameterTypeDefaultDescription
$table_namesarray—Unprefixed table identifiers (e.g. \’posts\’, \’commentmeta\’).
$enabledbool—Whether optimization is enabled.

Return: void.

Tags: @since 2.0.0

Source: includes/Database/class-database-cleanup.php, line 3353

Hooks

Hooks referenced in includes/Database/class-database-cleanup.php:

HookTypeLineNotes
wppo_revisions_time_budgetfilter384—
wppo_unattached_media_time_budgetfilter790—
wppo_core_autoload_optionsfilter1148@param ×1
wppo_autoload_critical_thresholdfilter1282@param ×1
wppo_purge_failed_actionsfilter2424@param ×1
wppo_action_scheduler_cleanup_enabledfilter2602@param ×1
wppo_database_cleanup_completedaction2694@param ×2
wppo_database_cleanup_completedaction2709—
wppo_database_cleanup_completedaction2712—