"Tamamlanan & başarısız kayıtları Şimdi Temizle" butnuna basınca yüzbinlerde sorgu anında temizlenir ve bunu her gece düzenli yapar. Bu süre kodu ilk çalıştırdığınızdan sonraki her gece 3600 saniye sonra otomatik çalışır.
Kodu wordpress sitenizde çalıştırmak için "Code Snippet" adlı eklentiyi kurun ve yeni kod parçacığı ekleyin.


/**
* Otomatik Veritabanı Stabilizasyon + Tek Tıkla Action Scheduler Temizleyici
*/
// 1. Tamamlanan Action Scheduler görevlerini 30 gün yerine sadece 1 gün tut ve toplu silme hızını artır
add_filter( 'action_scheduler_retention_period', function() {
return DAY_IN_SECONDS;
} );
add_filter( 'action_scheduler_cleanup_batch_size', function() {
return 1000;
} );
// 2. Sayfa/yazı sürümlerini maksimum 3 ile sınırla, ürünlerde sürüm tutma
add_filter( 'wp_revisions_to_keep', function( $num, $post ) {
if ( isset( $post->post_type ) && 'product' === $post->post_type ) {
return 0;
}
return 3;
}, 10, 2 );
// 3. Kilitlenmesiz Veritabanı & Action Scheduler Temizleme Motoru
function smpsoft_purge_action_scheduler_and_bloat( $purge_all_completed = false ) {
global $wpdb;
if ( function_exists( 'delete_expired_transients' ) ) {
delete_expired_transients( true );
}
$sessions_table = $wpdb->prefix . 'woocommerce_sessions';
if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $sessions_table ) ) === $sessions_table ) {
$wpdb->query( $wpdb->prepare( "DELETE FROM `{$sessions_table}` WHERE session_expiry < %d LIMIT 10000", time() ) );
}
$as_actions = $wpdb->prefix . 'actionscheduler_actions';
$as_logs = $wpdb->prefix . 'actionscheduler_logs';
$deleted_actions = 0;
$deleted_logs = 0;
if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $as_actions ) ) === $as_actions ) {
if ( $purge_all_completed ) {
do {
$rows = (int) $wpdb->query( "DELETE FROM `{$as_actions}` WHERE status IN ('complete', 'failed', 'canceled') LIMIT 25000" );
$deleted_actions += $rows;
} while ( $rows > 0 );
} else {
$cutoff = gmdate( 'Y-m-d H:i:s', time() - DAY_IN_SECONDS );
do {
$rows = (int) $wpdb->query( $wpdb->prepare(
"DELETE FROM `{$as_actions}` WHERE status IN ('complete', 'failed', 'canceled') AND scheduled_date_gmt < %s LIMIT 25000",
$cutoff
) );
$deleted_actions += $rows;
} while ( $rows > 0 );
}
if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $as_logs ) ) === $as_logs ) {
$keep_ids = $wpdb->get_col( "SELECT action_id FROM `{$as_actions}`" );
if ( empty( $keep_ids ) ) {
do {
$lrows = (int) $wpdb->query( "DELETE FROM `{$as_logs}` LIMIT 50000" );
$deleted_logs += $lrows;
} while ( $lrows > 0 );
} elseif ( count( $keep_ids ) <= 5000 ) {
$in_sql = implode( ',', array_map( 'absint', $keep_ids ) );
do {
$lrows = (int) $wpdb->query( "DELETE FROM `{$as_logs}` WHERE action_id NOT IN ({$in_sql}) LIMIT 50000" );
$deleted_logs += $lrows;
} while ( $lrows > 0 );
} else {
for ( $i = 0; $i < 10; $i++ ) {
$orphan_log_ids = $wpdb->get_col(
"SELECT l.log_id FROM `{$as_logs}` l LEFT JOIN `{$as_actions}` a ON a.action_id = l.action_id WHERE a.action_id IS NULL LIMIT 10000"
);
if ( empty( $orphan_log_ids ) ) {
break;
}
$ids_sql = implode( ',', array_map( 'absint', $orphan_log_ids ) );
$deleted_logs += (int) $wpdb->query( "DELETE FROM `{$as_logs}` WHERE log_id IN ({$ids_sql})" );
}
}
}
if ( $purge_all_completed ) {
$wpdb->query( "OPTIMIZE TABLE `{$as_actions}`, `{$as_logs}`" );
}
}
$wpdb->query( "DELETE FROM `{$wpdb->postmeta}` WHERE meta_key IN ('_edit_lock', '_edit_last') LIMIT 10000" );
$orphan_meta_ids = $wpdb->get_col(
"SELECT pm.meta_id FROM `{$wpdb->postmeta}` pm LEFT JOIN `{$wpdb->posts}` p ON p.ID = pm.post_id WHERE p.ID IS NULL LIMIT 5000"
);
if ( ! empty( $orphan_meta_ids ) ) {
$meta_ids_sql = implode( ',', array_map( 'absint', $orphan_meta_ids ) );
$wpdb->query( "DELETE FROM `{$wpdb->postmeta}` WHERE meta_id IN ({$meta_ids_sql})" );
}
return array(
'actions' => $deleted_actions,
'logs' => $deleted_logs,
);
}
// 4. Her gün otomatik arka plan bakımı
add_action( 'init', function() {
if ( ! wp_next_scheduled( 'smpsoft_daily_db_maintenance' ) ) {
wp_schedule_event( time() + 3600, 'daily', 'smpsoft_daily_db_maintenance' );
}
} );
add_action( 'smpsoft_daily_db_maintenance', function() {
smpsoft_purge_action_scheduler_and_bloat( false );
} );
// 5. Zamanlanmış Eylemler ekranına "Şimdi Temizle" butonu ekle
add_action( 'admin_init', function() {
if ( ! isset( $_GET['smpsoft_clean_as'] ) || ! current_user_can( 'manage_options' ) ) {
return;
}
check_admin_referer( 'smpsoft_clean_as_nonce' );
if ( function_exists( 'set_time_limit' ) ) {
@set_time_limit( 120 );
}
$stats = smpsoft_purge_action_scheduler_and_bloat( true );
wp_safe_redirect( add_query_arg(
array(
'page' => 'wc-status',
'tab' => 'action-scheduler',
'as_cleaned' => 1,
'del_actions' => (int) $stats['actions'],
'del_logs' => (int) $stats['logs'],
),
admin_url( 'admin.php' )
) );
exit;
} );
add_action( 'admin_notices', function() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$page = isset( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : '';
$tab = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : '';
if ( 'wc-status' !== $page || 'action-scheduler' !== $tab ) {
return;
}
if ( isset( $_GET['as_cleaned'] ) ) {
$da = isset( $_GET['del_actions'] ) ? absint( $_GET['del_actions'] ) : 0;
$dl = isset( $_GET['del_logs'] ) ? absint( $_GET['del_logs'] ) : 0;
echo '<div class="notice notice-success is-dismissible"><p><strong>✓ Temizlik Tamamlandı:</strong> ' . esc_html( number_format_i18n( $da ) ) . ' tamamlanmış/başarısız eylem ve ' . esc_html( number_format_i18n( $dl ) ) . ' günlük (log) kaydı silinip tablolar optimize edildi. Bekleyen zamanlanmış görevleriniz korundu.</p></div>';
}
$clean_url = wp_nonce_url(
add_query_arg(
array(
'page' => 'wc-status',
'tab' => 'action-scheduler',
'smpsoft_clean_as' => '1',
),
admin_url( 'admin.php' )
),
'smpsoft_clean_as_nonce'
);
echo '<div class="notice notice-info" style="display:flex;align-items:center;justify-content:space-between;gap:16px;padding:12px 16px;">';
echo '<span><strong>Veritabanı Hızlandırıcı:</strong> Tamamlanmış ve başarısız eski eylemleri (Bekleyen görevlere dokunmadan) tek tıkla temizleyebilirsiniz.</span>';
echo '<a href="' . esc_url( $clean_url ) . '" class="button button-primary">Tamamlanan & Başarısız Kayıtları Şimdi Temizle</a>';
echo '</div>';
} );