Title: Clear cache via php code
Last modified: November 18, 2022

---

# Clear cache via php code

 *  Resolved [yydevelopment](https://wordpress.org/support/users/yydevelopment/)
 * (@yydevelopment)
 * [3 years, 6 months ago](https://wordpress.org/support/topic/clear-cache-via-php-code/)
 * Hi, i have few plugins I developed and a lot of custom code i created myself.
 * Is there a way for me to run php function and clear your cache without the button
   on the top bar?
    Also is there a CLI command that allow to do that as well?
 * Will be great if it’s possible for regular cache and cloudflare cache.
 * Thanks

Viewing 5 replies - 1 through 5 (of 5 total)

 *  [serpentdriver](https://wordpress.org/support/users/serpentdriver/)
 * (@serpentdriver)
 * [3 years, 6 months ago](https://wordpress.org/support/topic/clear-cache-via-php-code/#post-16211378)
 * If you want to purge the cache created by the plugin:
 *     ```
       <?php
       $tag = substr(md5(__DIR__ . '/system/library/lscache'),0,4);
       header("X-LiteSpeed-Purge:" . $tag);
       ```
   
 * If you want to purge the entire cache:
 *     ```
       <?php
       header("X-LiteSpeed-Purge:*");
       ```
   
 * To purge the cache only from CLI:
 *     ```
       if (php_sapi_name() === 'cli') {
       $path = dirname(__FILE__);
       $tag = __DIR__ . '/wp-content/plugins/litespeed-cache/';
       $tag = substr(md5($tag), -3) . '_';
       header("X-LiteSpeed-Purge:" . $tag);
       }else{
       header('HTTP/1.0 403 Forbidden');
       $contents = file_get_contents($path . '/403.shtml', TRUE);
       exit($contents);
       }
       ```
   
 * 403.shtml
 *     ```
       <!DOCTYPE html>
       <html style="height:100%">
       <head>
       <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" >
       <title> 403 Forbidden
       </title></head>
       <body style="color: #444; margin:0;font: normal 14px/20px Arial, Helvetica, sans-serif; height:100%; background-color: #fff;">
       <div style="height:auto; min-height:100%; ">     
       <div style="text-align: center; width:800px; margin-left: -400px; position:absolute; top: 30%; left:50%;"><h1 style="margin:0; font-size:150px; line-height:150px; font-weight:bold;">403</h1>
       <h2 style="margin-top:20px;font-size: 30px;">Forbidden</h2>
       <p>Access to this resource on the server is denied!</p>
       </div>
       </div>
       </body>
       </html>
       ```
   
 * Copy the code from above into a blank PHP file and place it in document_root.
    -  This reply was modified 3 years, 6 months ago by [serpentdriver](https://wordpress.org/support/users/serpentdriver/).
 *  Plugin Support [qtwrk](https://wordpress.org/support/users/qtwrk/)
 * (@qtwrk)
 * [3 years, 6 months ago](https://wordpress.org/support/topic/clear-cache-via-php-code/#post-16213758)
 *     ```
       <?php
       require( './wp-load.php' );
       if ( defined('LSCWP_V')) {
         do_action( 'litespeed_purge_all' );
         echo 'done';
       }
       ```
   
 * can use this one , it will call plugin’s purge API
 *  Thread Starter [yydevelopment](https://wordpress.org/support/users/yydevelopment/)
 * (@yydevelopment)
 * [3 years, 6 months ago](https://wordpress.org/support/topic/clear-cache-via-php-code/#post-16214324)
 * Thanks for both of you,
    I sometimes update the sites via SSH,
 * Is there a command i can use there as well to clear everything or does it require
   access to php and wp-load.php?
 * Also does do_action( ‘litespeed_purge_all’ ); clear cloudflare as well?
    Does
   the api code offer a way to clear cloudflare as well or only the plugin cache?
 *  Plugin Support [qtwrk](https://wordpress.org/support/users/qtwrk/)
 * (@qtwrk)
 * [3 years, 6 months ago](https://wordpress.org/support/topic/clear-cache-via-php-code/#post-16214342)
 * you can run `rm -rf /path/to/your/lscache/` but not suggested , as `rm` command
   is always dangerous
 * it is better to use `do_action( ‘litespeed_purge_all’ );` as it also clears object
   cache , generated JS/CSS files …etc
 * no , it doesn’t purge CF
 *  Thread Starter [yydevelopment](https://wordpress.org/support/users/yydevelopment/)
 * (@yydevelopment)
 * [3 years, 6 months ago](https://wordpress.org/support/topic/clear-cache-via-php-code/#post-16257670)
 * Thank you and sorry about the late reply.
    So just making sure, is there an action
   i can use to clear cloudflare as well?

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Clear cache via php code’ is closed to new replies.

 * ![](https://ps.w.org/litespeed-cache/assets/icon-256x256.png?rev=2554181)
 * [LiteSpeed Cache](https://wordpress.org/plugins/litespeed-cache/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/litespeed-cache/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/litespeed-cache/)
 * [Active Topics](https://wordpress.org/support/plugin/litespeed-cache/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/litespeed-cache/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/litespeed-cache/reviews/)

 * 5 replies
 * 3 participants
 * Last reply from: [yydevelopment](https://wordpress.org/support/users/yydevelopment/)
 * Last activity: [3 years, 6 months ago](https://wordpress.org/support/topic/clear-cache-via-php-code/#post-16257670)
 * Status: resolved