{"id":515,"date":"2026-09-30T08:35:00","date_gmt":"2026-09-30T06:35:00","guid":{"rendered":"https:\/\/www.lukaswojcik.com\/?page_id=515"},"modified":"2026-09-25T15:07:51","modified_gmt":"2026-09-25T13:07:51","slug":"automated-database-cleanup-via-wp-cli-and-server-cron-jobs","status":"publish","type":"post","link":"https:\/\/www.lukaswojcik.com\/blog\/en\/wordpress-plugins-tricks\/tutorials-en-wordpress-plugins-tricks\/automated-database-cleanup-via-wp-cli-and-server-cron-jobs\/","title":{"rendered":"Automated Database Cleanup via WP-CLI and Server Cron Jobs"},"content":{"rendered":"<h2>Architectural Overview: Database Fragmentation in WordPress<\/h2>\n<p>As a WordPress v7.0.2 installation matures, its underlying MySQL or MariaDB database undergoes significant fragmentation and data bloat. Every editorial save generates a new row in the <code>wp_posts<\/code> table as a post revision. Concurrently, plugins and themes frequently write temporary cache data to the <code>wp_options<\/code> table as transients. When these transients expire, WordPress relies on lazy-deletion mechanisms during HTTP requests, often leaving thousands of obsolete entries behind.<\/p>\n<p>Additionally, deleting posts or custom post types without explicit cleanup routines results in orphaned metadata inside the <code>wp_postmeta<\/code> table. Over time, this uncontrolled accumulation inflates index sizes, slows down SQL execution times for complex <code>SELECT<\/code> queries, and increases server memory consumption. Automating regular maintenance via WP-CLI and server-level cron jobs bypasses the web server stack entirely, ensuring high-speed, non-blocking cleanup without hitting PHP execution time limits.<\/p>\n<figure class=\"lw-diagram\">\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.lukaswojcik.com\/blog\/wp-content\/uploads\/diagrams\/hand-dbputz-en.png\" width=\"1120\" height=\"620\" alt=\"Stacked before-and-after comparison of a WordPress database with the five cleanup steps in between\"><figcaption>Four kinds of ballast make up the bulk of a grown WordPress database. The weekly script removes them in a fixed order and defragments afterwards, which leaves only the content that is actually served.<\/figcaption><\/figure>\n<h2>Step-by-Step Implementation Guide<\/h2>\n<h3>Step 1: Identifying Database Bloat and Performing an Initial CLI Audit<\/h3>\n<p>Before executing deletion routines, the current size and structure of the database should be inspected using WP-CLI via an SSH terminal. The following command displays an overview of all tables sorted by their total size in megabytes:<\/p>\n<pre><code>wp db size --tables --human-readable<\/code><\/pre>\n<p>To quantify the exact number of expired transients currently lingering in the options table, an initial inspection query can be executed:<\/p>\n<pre><code>wp db query \"SELECT COUNT(*) FROM wp_options WHERE option_name LIKE '_transient_timeout_%' AND option_value &lt; UNIX_TIMESTAMP();\"<\/code><\/pre>\n<h3>Step 2: Designing the Automated Maintenance Script<\/h3>\n<p>To safely eliminate dead data without relying on third-party WordPress plugins, a dedicated shell script utilizing WP-CLI and direct database queries is optimal. The script addresses four primary sources of bloat:<\/p>\n<ul>\n<li><strong>Expired Transients:<\/strong> Flushes all outdated transient entries from <code>wp_options<\/code>.<\/li>\n<li><strong>Old Post Revisions:<\/strong> Purges historical revision objects from <code>wp_posts<\/code>.<\/li>\n<li><strong>Orphaned Postmeta:<\/strong> Executes a relational <code>DELETE<\/code> query to remove metadata whose parent post ID no longer exists.<\/li>\n<li><strong>Orphaned Term Relationships:<\/strong> Removes taxonomy assignments in <code>wp_term_relationships<\/code> whose referenced object no longer exists. With Polylang, or with taxonomies registered for other object types, the query also deletes assignments that do not belong to a post at all, such as the language assignments of categories and tags; details under \u201cQuestions and answers\u201d.<\/li>\n<\/ul>\n<p>The following production-ready script can be saved on the server as <code>\/opt\/scripts\/wp-db-cleanup.sh<\/code>:<\/p>\n<pre><code>#!\/usr\/bin\/env bash\n# ============================================================================\n# High-Performance WordPress Database Cleanup Script\n# Target: WordPress v7.0.2 via WP-CLI\n# ============================================================================\n\nWP_PATH=\"\/var\/www\/lukaswojcik.com\/htdocs\"\nLOG_FILE=\"\/var\/log\/wp_db_cleanup.log\"\n\necho \"[$(date +'%Y-%m-%d %H:%M:%S')] Starting maintenance cycle...\" &gt;&gt; \"$LOG_FILE\"\n\n# 1. Purge all expired transients\nwp transient delete --expired --path=\"$WP_PATH\" &gt;&gt; \"$LOG_FILE\" 2&gt;&amp;1\n\n# 2. Delete all post revisions (optional: keep latest 3 revisions per post if needed)\nREVISIONS=$(wp post list --post_type=revision --format=ids --path=\"$WP_PATH\")\nif [ -n \"$REVISIONS\" ]; then\n    wp post delete $REVISIONS --force --path=\"$WP_PATH\" &gt;&gt; \"$LOG_FILE\" 2&gt;&amp;1\nfi\n\n# 3. Remove orphaned postmeta entries via direct relational SQL\nwp db query \"DELETE pm FROM wp_postmeta pm LEFT JOIN wp_posts wp ON wp.ID = pm.post_id WHERE wp.ID IS NULL;\" --path=\"$WP_PATH\" &gt;&gt; \"$LOG_FILE\" 2&gt;&amp;1\n\n# 4. Remove orphaned term relationships\n# Caution: with Polylang this also deletes term language assignments (object_id = term ID)\nwp db query \"DELETE tr FROM wp_term_relationships tr LEFT JOIN wp_posts wp ON wp.ID = tr.object_id WHERE wp.ID IS NULL;\" --path=\"$WP_PATH\" &gt;&gt; \"$LOG_FILE\" 2&gt;&amp;1\n\n# 5. Defragment and optimize MySQL\/MariaDB storage engines\nwp db optimize --path=\"$WP_PATH\" &gt;&gt; \"$LOG_FILE\" 2&gt;&amp;1\n\necho \"[$(date +'%Y-%m-%d %H:%M:%S')] Maintenance completed successfully.\" &gt;&gt; \"$LOG_FILE\"<\/code><\/pre>\n<h3>Step 3: Enforcing Strict Script Permissions<\/h3>\n<p>To prevent unauthorized execution, the shell script must be assigned appropriate Unix filesystem permissions. Execution rights should be restricted strictly to the administrative user or the web server service account:<\/p>\n<pre><code>chmod 700 \/opt\/scripts\/wp-db-cleanup.sh\nchown www-data:www-data \/opt\/scripts\/wp-db-cleanup.sh<\/code><\/pre>\n<h3>Step 4: Automating Execution via Server Cron Job<\/h3>\n<p>Unlike WP-Cron, which depends on incoming HTTP web traffic and can be delayed on low-traffic sites, a native Linux system cron job guarantees execution at precise intervals. To schedule the cleanup routine to run every Sunday at 03:00 AM, the crontab configuration must be edited:<\/p>\n<pre><code>crontab -e -u www-data<\/code><\/pre>\n<p>The following schedule expression is appended to the cron configuration file:<\/p>\n<pre><code>0 3 * * 0 \/bin\/bash \/opt\/scripts\/wp-db-cleanup.sh &gt;\/dev\/null 2&gt;&amp;1<\/code><\/pre>\n<h3>Step 5: Quality Assurance and Verification<\/h3>\n<p>After manual invocation of the shell script, verification should be performed via terminal commands:<\/p>\n<ol>\n<li><strong>Check Execution Logs:<\/strong> Review `\/var\/log\/wp_db_cleanup.log` to confirm zero syntax errors or database locks occurred.<\/li>\n<li><strong>Validate Table Optimization:<\/strong> Execute `wp db size &#8211;human-readable` and observe the reduced storage footprint of `wp_postmeta` and `wp_posts`.<\/li>\n<li><strong>Inspect Frontend Integrity:<\/strong> Verify that active pages, custom toolbox tools, and Polylang language relations load seamlessly without missing metadata.<\/li>\n<\/ol>\n<h2>Summary and Measurable Added Value<\/h2>\n<p><strong>What is achieved:<\/strong> Replacement of manual database maintenance and resource-heavy optimization plugins with an autonomous, server-level maintenance pipeline using WP-CLI and Linux cron jobs.<\/p>\n<p><strong>Resulting added value:<\/strong><\/p>\n<ul>\n<li><strong>Permanent Database Leanings:<\/strong> Relational tables remain defragmented and free of orphaned metadata, reducing overall database size by up to 40\u201360% on active publishing sites.<\/li>\n<li><strong>Accelerated SQL Query Speed:<\/strong> Cleaned indexes lead to significantly lower query execution latencies for dynamic dashboard requests and complex JOIN operations.<\/li>\n<li><strong>Zero PHP Resource Overhead:<\/strong> Executing maintenance directly via CLI eliminates HTTP timeouts, memory spikes, and backend slowdowns for frontend visitors.<\/li>\n<\/ul>\n<div class=\"lw-faq\">\n<h2>Questions and answers<\/h2>\n<h3>Why does the script run cleanly by hand but not from the cron job?<\/h3>\n<p>Usually because of a different environment. Cron starts commands with a minimal environment, and three differences are the most common causes:<\/p>\n<ol>\n<li>The search path. Cron usually sets PATH to just \/usr\/bin:\/bin. If WP-CLI lives at \/usr\/local\/bin\/wp, as it often does, the script cannot find the wp command. A full path in the script or a PATH line at the top of the crontab fixes this.<\/li>\n<li>Write access to the log file. The script runs as www-data and writes to \/var\/log\/wp_db_cleanup.log, in a directory www-data normally cannot write to. If the redirection to the log fails, the shell does not run the command in question at all, so the database is never touched. The file therefore has to be created beforehand and handed to www-data, or the log has to live somewhere www-data may write.<\/li>\n<li>The swallowed output. The crontab line sends everything to \/dev\/null. The error messages from the failed redirections disappear there, and the run looks silently successful.<\/li>\n<\/ol>\n<p>A test run under the same conditions exposes these errors in advance, for example with <code>sudo -u www-data env -i PATH=\/usr\/bin:\/bin \/bin\/bash \/opt\/scripts\/wp-db-cleanup.sh<\/code>. If the script completes that way, the most common causes are ruled out.<\/p>\n<h3>Is the query for orphaned term relationships safe on every installation?<\/h3>\n<p>No. The wp_term_relationships table does not only link terms to posts. A taxonomy can be registered for other object types, such as users or the old links in wp_links, and then object_id does not hold a post ID. Multilingual plugins such as Polylang also assign categories and tags to their language and translations through this table, with the term ID as object_id.<\/p>\n<p>The query in the script deletes every row whose object_id is missing from wp_posts and thus hits exactly these assignments. The treacherous part is that it does not hit all of them: a term ID that happens to exist as a post ID as well survives. The result is scattered categories without a language or without a translation, which are hard to explain.<\/p>\n<p>It is safer to restrict the query to the taxonomies actually attached to posts, via a JOIN on wp_term_taxonomy and a list such as category and post_tag. Every deletion run should also be preceded by a backup, for example with <code>wp db export<\/code>, and a dry run with SELECT COUNT(*) instead of DELETE shows how many rows would be affected.<\/p>\n<h3>What does wp db optimize do to InnoDB tables?<\/h3>\n<p>InnoDB has no optimization of its own. MySQL and MariaDB rebuild the table instead and update its statistics; the message reads \u201cTable does not support optimize, doing recreate + analyze instead\u201d. For the rebuild the server temporarily needs about as much free disk space as the table occupies, and large tables take correspondingly long.<\/p>\n<p>The file only shrinks if every table has a file of its own (innodb_file_per_table, the default in current versions). If the tables sit in the shared system tablespace, its file stays the same size. After a large deletion the rebuild is worthwhile; as a weekly routine it achieves little, because InnoDB reuses freed space within the table anyway.<\/p>\n<h3>Can the number of revisions be capped instead of deleting them all every week?<\/h3>\n<p>Yes, with the WP_POST_REVISIONS constant in wp-config.php, for example <code>define( 'WP_POST_REVISIONS', 3 );<\/code>. WordPress then removes a post&#8217;s oldest revisions beyond the limit the next time that post is saved. The version history is kept that way, while the script deletes it completely every week.<\/p>\n<\/div>\n<div class=\"lw-quellen\">\n<h2>Sources<\/h2>\n<ul>\n<li><a href=\"https:\/\/wordpress.org\/cli\/\" target=\"_blank\" rel=\"noopener noreferrer\">WP-CLI documentation<\/a><\/li>\n<li><a href=\"https:\/\/man7.org\/linux\/man-pages\/man5\/crontab.5.html\" target=\"_blank\" rel=\"noopener noreferrer\">crontab manual (man7.org)<\/a><\/li>\n<\/ul>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Technical tutorial on permanently maintaining lean WordPress v7.0.2 MySQL\/MariaDB databases by automating the deletion of orphaned postmeta, old revisions, and expired transients using WP-CLI.<\/p>\n","protected":false},"author":1,"featured_media":14067,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[92642],"tags":[91106,91104,91099,91219,91096],"class_list":["post-515","post","type-post","status-publish","format-standard","hentry","category-tutorials-en-wordpress-plugins-tricks","tag-automation","tag-mysql","tag-server-administration","tag-tutorial","tag-wordpress"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Automated Database Cleanup via WP-CLI and Server Cron Jobs | Lukas Wojcik<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.lukaswojcik.com\/blog\/en\/wordpress-plugins-tricks\/tutorials-en-wordpress-plugins-tricks\/automated-database-cleanup-via-wp-cli-and-server-cron-jobs\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Automated Database Cleanup via WP-CLI and Server Cron Jobs | Lukas Wojcik\" \/>\n<meta property=\"og:description\" content=\"Technical tutorial on permanently maintaining lean WordPress v7.0.2 MySQL\/MariaDB databases by automating the deletion of orphaned postmeta, old revisions, and expired transients using WP-CLI.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.lukaswojcik.com\/blog\/en\/wordpress-plugins-tricks\/tutorials-en-wordpress-plugins-tricks\/automated-database-cleanup-via-wp-cli-and-server-cron-jobs\/\" \/>\n<meta property=\"og:site_name\" content=\"Lukas Wojcik - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-30T06:35:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.lukaswojcik.com\/blog\/wp-content\/uploads\/2026\/09\/hero-515-automated-database-cleanup-wp-cli-cr-g.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Lukas Wojcik\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Lukas Wojcik\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/en\\\/wordpress-plugins-tricks\\\/tutorials-en-wordpress-plugins-tricks\\\/automated-database-cleanup-via-wp-cli-and-server-cron-jobs\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/en\\\/wordpress-plugins-tricks\\\/tutorials-en-wordpress-plugins-tricks\\\/automated-database-cleanup-via-wp-cli-and-server-cron-jobs\\\/\"},\"author\":{\"name\":\"Lukas Wojcik\",\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/#\\\/schema\\\/person\\\/895f7604f9b6b71aad9bba33af28d0f9\"},\"headline\":\"Automated Database Cleanup via WP-CLI and Server Cron Jobs\",\"datePublished\":\"2026-09-30T06:35:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/en\\\/wordpress-plugins-tricks\\\/tutorials-en-wordpress-plugins-tricks\\\/automated-database-cleanup-via-wp-cli-and-server-cron-jobs\\\/\"},\"wordCount\":1257,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/#\\\/schema\\\/person\\\/895f7604f9b6b71aad9bba33af28d0f9\"},\"image\":{\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/en\\\/wordpress-plugins-tricks\\\/tutorials-en-wordpress-plugins-tricks\\\/automated-database-cleanup-via-wp-cli-and-server-cron-jobs\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/hero-515-automated-database-cleanup-wp-cli-cr-g.png\",\"keywords\":[\"Automation\",\"MySQL\",\"Server Administration\",\"Tutorial\",\"WordPress\"],\"articleSection\":[\"Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/en\\\/wordpress-plugins-tricks\\\/tutorials-en-wordpress-plugins-tricks\\\/automated-database-cleanup-via-wp-cli-and-server-cron-jobs\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/en\\\/wordpress-plugins-tricks\\\/tutorials-en-wordpress-plugins-tricks\\\/automated-database-cleanup-via-wp-cli-and-server-cron-jobs\\\/\",\"url\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/en\\\/wordpress-plugins-tricks\\\/tutorials-en-wordpress-plugins-tricks\\\/automated-database-cleanup-via-wp-cli-and-server-cron-jobs\\\/\",\"name\":\"Automated Database Cleanup via WP-CLI and Server Cron Jobs | Lukas Wojcik\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/en\\\/wordpress-plugins-tricks\\\/tutorials-en-wordpress-plugins-tricks\\\/automated-database-cleanup-via-wp-cli-and-server-cron-jobs\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/en\\\/wordpress-plugins-tricks\\\/tutorials-en-wordpress-plugins-tricks\\\/automated-database-cleanup-via-wp-cli-and-server-cron-jobs\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/hero-515-automated-database-cleanup-wp-cli-cr-g.png\",\"datePublished\":\"2026-09-30T06:35:00+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/en\\\/wordpress-plugins-tricks\\\/tutorials-en-wordpress-plugins-tricks\\\/automated-database-cleanup-via-wp-cli-and-server-cron-jobs\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/en\\\/wordpress-plugins-tricks\\\/tutorials-en-wordpress-plugins-tricks\\\/automated-database-cleanup-via-wp-cli-and-server-cron-jobs\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/en\\\/wordpress-plugins-tricks\\\/tutorials-en-wordpress-plugins-tricks\\\/automated-database-cleanup-via-wp-cli-and-server-cron-jobs\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/hero-515-automated-database-cleanup-wp-cli-cr-g.png\",\"contentUrl\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/hero-515-automated-database-cleanup-wp-cli-cr-g.png\",\"width\":1200,\"height\":630,\"caption\":\"Automated Database Cleanup via WP-CLI and Server Cron Jobs\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/en\\\/wordpress-plugins-tricks\\\/tutorials-en-wordpress-plugins-tricks\\\/automated-database-cleanup-via-wp-cli-and-server-cron-jobs\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Automated Database Cleanup via WP-CLI and Server Cron Jobs\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/\",\"name\":\"Lukas Wojcik - Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/#\\\/schema\\\/person\\\/895f7604f9b6b71aad9bba33af28d0f9\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/#\\\/schema\\\/person\\\/895f7604f9b6b71aad9bba33af28d0f9\",\"name\":\"Lukas Wojcik\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/lw-x2.jpg\",\"url\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/lw-x2.jpg\",\"contentUrl\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/lw-x2.jpg\",\"width\":424,\"height\":636,\"caption\":\"Lukas Wojcik\"},\"logo\":{\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/lw-x2.jpg\"},\"sameAs\":[\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Automated Database Cleanup via WP-CLI and Server Cron Jobs | Lukas Wojcik","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.lukaswojcik.com\/blog\/en\/wordpress-plugins-tricks\/tutorials-en-wordpress-plugins-tricks\/automated-database-cleanup-via-wp-cli-and-server-cron-jobs\/","og_locale":"en_US","og_type":"article","og_title":"Automated Database Cleanup via WP-CLI and Server Cron Jobs | Lukas Wojcik","og_description":"Technical tutorial on permanently maintaining lean WordPress v7.0.2 MySQL\/MariaDB databases by automating the deletion of orphaned postmeta, old revisions, and expired transients using WP-CLI.","og_url":"https:\/\/www.lukaswojcik.com\/blog\/en\/wordpress-plugins-tricks\/tutorials-en-wordpress-plugins-tricks\/automated-database-cleanup-via-wp-cli-and-server-cron-jobs\/","og_site_name":"Lukas Wojcik - Blog","article_published_time":"2026-09-30T06:35:00+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/www.lukaswojcik.com\/blog\/wp-content\/uploads\/2026\/09\/hero-515-automated-database-cleanup-wp-cli-cr-g.png","type":"image\/png"}],"author":"Lukas Wojcik","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Lukas Wojcik","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.lukaswojcik.com\/blog\/en\/wordpress-plugins-tricks\/tutorials-en-wordpress-plugins-tricks\/automated-database-cleanup-via-wp-cli-and-server-cron-jobs\/#article","isPartOf":{"@id":"https:\/\/www.lukaswojcik.com\/blog\/en\/wordpress-plugins-tricks\/tutorials-en-wordpress-plugins-tricks\/automated-database-cleanup-via-wp-cli-and-server-cron-jobs\/"},"author":{"name":"Lukas Wojcik","@id":"https:\/\/www.lukaswojcik.com\/blog\/#\/schema\/person\/895f7604f9b6b71aad9bba33af28d0f9"},"headline":"Automated Database Cleanup via WP-CLI and Server Cron Jobs","datePublished":"2026-09-30T06:35:00+00:00","mainEntityOfPage":{"@id":"https:\/\/www.lukaswojcik.com\/blog\/en\/wordpress-plugins-tricks\/tutorials-en-wordpress-plugins-tricks\/automated-database-cleanup-via-wp-cli-and-server-cron-jobs\/"},"wordCount":1257,"commentCount":0,"publisher":{"@id":"https:\/\/www.lukaswojcik.com\/blog\/#\/schema\/person\/895f7604f9b6b71aad9bba33af28d0f9"},"image":{"@id":"https:\/\/www.lukaswojcik.com\/blog\/en\/wordpress-plugins-tricks\/tutorials-en-wordpress-plugins-tricks\/automated-database-cleanup-via-wp-cli-and-server-cron-jobs\/#primaryimage"},"thumbnailUrl":"https:\/\/www.lukaswojcik.com\/blog\/wp-content\/uploads\/2026\/09\/hero-515-automated-database-cleanup-wp-cli-cr-g.png","keywords":["Automation","MySQL","Server Administration","Tutorial","WordPress"],"articleSection":["Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.lukaswojcik.com\/blog\/en\/wordpress-plugins-tricks\/tutorials-en-wordpress-plugins-tricks\/automated-database-cleanup-via-wp-cli-and-server-cron-jobs\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.lukaswojcik.com\/blog\/en\/wordpress-plugins-tricks\/tutorials-en-wordpress-plugins-tricks\/automated-database-cleanup-via-wp-cli-and-server-cron-jobs\/","url":"https:\/\/www.lukaswojcik.com\/blog\/en\/wordpress-plugins-tricks\/tutorials-en-wordpress-plugins-tricks\/automated-database-cleanup-via-wp-cli-and-server-cron-jobs\/","name":"Automated Database Cleanup via WP-CLI and Server Cron Jobs | Lukas Wojcik","isPartOf":{"@id":"https:\/\/www.lukaswojcik.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.lukaswojcik.com\/blog\/en\/wordpress-plugins-tricks\/tutorials-en-wordpress-plugins-tricks\/automated-database-cleanup-via-wp-cli-and-server-cron-jobs\/#primaryimage"},"image":{"@id":"https:\/\/www.lukaswojcik.com\/blog\/en\/wordpress-plugins-tricks\/tutorials-en-wordpress-plugins-tricks\/automated-database-cleanup-via-wp-cli-and-server-cron-jobs\/#primaryimage"},"thumbnailUrl":"https:\/\/www.lukaswojcik.com\/blog\/wp-content\/uploads\/2026\/09\/hero-515-automated-database-cleanup-wp-cli-cr-g.png","datePublished":"2026-09-30T06:35:00+00:00","breadcrumb":{"@id":"https:\/\/www.lukaswojcik.com\/blog\/en\/wordpress-plugins-tricks\/tutorials-en-wordpress-plugins-tricks\/automated-database-cleanup-via-wp-cli-and-server-cron-jobs\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.lukaswojcik.com\/blog\/en\/wordpress-plugins-tricks\/tutorials-en-wordpress-plugins-tricks\/automated-database-cleanup-via-wp-cli-and-server-cron-jobs\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.lukaswojcik.com\/blog\/en\/wordpress-plugins-tricks\/tutorials-en-wordpress-plugins-tricks\/automated-database-cleanup-via-wp-cli-and-server-cron-jobs\/#primaryimage","url":"https:\/\/www.lukaswojcik.com\/blog\/wp-content\/uploads\/2026\/09\/hero-515-automated-database-cleanup-wp-cli-cr-g.png","contentUrl":"https:\/\/www.lukaswojcik.com\/blog\/wp-content\/uploads\/2026\/09\/hero-515-automated-database-cleanup-wp-cli-cr-g.png","width":1200,"height":630,"caption":"Automated Database Cleanup via WP-CLI and Server Cron Jobs"},{"@type":"BreadcrumbList","@id":"https:\/\/www.lukaswojcik.com\/blog\/en\/wordpress-plugins-tricks\/tutorials-en-wordpress-plugins-tricks\/automated-database-cleanup-via-wp-cli-and-server-cron-jobs\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.lukaswojcik.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Automated Database Cleanup via WP-CLI and Server Cron Jobs"}]},{"@type":"WebSite","@id":"https:\/\/www.lukaswojcik.com\/blog\/#website","url":"https:\/\/www.lukaswojcik.com\/blog\/","name":"Lukas Wojcik - Blog","description":"","publisher":{"@id":"https:\/\/www.lukaswojcik.com\/blog\/#\/schema\/person\/895f7604f9b6b71aad9bba33af28d0f9"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.lukaswojcik.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.lukaswojcik.com\/blog\/#\/schema\/person\/895f7604f9b6b71aad9bba33af28d0f9","name":"Lukas Wojcik","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.lukaswojcik.com\/blog\/wp-content\/uploads\/2026\/07\/lw-x2.jpg","url":"https:\/\/www.lukaswojcik.com\/blog\/wp-content\/uploads\/2026\/07\/lw-x2.jpg","contentUrl":"https:\/\/www.lukaswojcik.com\/blog\/wp-content\/uploads\/2026\/07\/lw-x2.jpg","width":424,"height":636,"caption":"Lukas Wojcik"},"logo":{"@id":"https:\/\/www.lukaswojcik.com\/blog\/wp-content\/uploads\/2026\/07\/lw-x2.jpg"},"sameAs":["https:\/\/www.lukaswojcik.com\/blog"]}]}},"_links":{"self":[{"href":"https:\/\/www.lukaswojcik.com\/blog\/wp-json\/wp\/v2\/posts\/515","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.lukaswojcik.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lukaswojcik.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lukaswojcik.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lukaswojcik.com\/blog\/wp-json\/wp\/v2\/comments?post=515"}],"version-history":[{"count":5,"href":"https:\/\/www.lukaswojcik.com\/blog\/wp-json\/wp\/v2\/posts\/515\/revisions"}],"predecessor-version":[{"id":20942,"href":"https:\/\/www.lukaswojcik.com\/blog\/wp-json\/wp\/v2\/posts\/515\/revisions\/20942"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.lukaswojcik.com\/blog\/wp-json\/wp\/v2\/media\/14067"}],"wp:attachment":[{"href":"https:\/\/www.lukaswojcik.com\/blog\/wp-json\/wp\/v2\/media?parent=515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lukaswojcik.com\/blog\/wp-json\/wp\/v2\/categories?post=515"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lukaswojcik.com\/blog\/wp-json\/wp\/v2\/tags?post=515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}