#!/bin/bash #################################################################################################### #### author: SlickStack ############################################################################ #### link: https://slickstack.io ################################################################### #### mirror: http://mirrors.slickstack.io/bash/ss-purge.txt ######################################## #### path: /var/www/ss-purge ####################################################################### #### destination: n/a (not a boilerplate) ########################################################## #### purpose: Clears all caches i.e. Nginx (FastCGI), PHP OPcache, Redis (object cache), etc ####### #### module version: Nginx 1.18.x + PHP-FPM 7.4.x + Redis 5.0.x + MySQL 8.0.x ###################### #### sourced by: ss core bash scripts ############################################################## #### bash aliases: ss purge, ss purge caches, ss clear caches ###################################### #################################################################################################### ## RUNNING SS-PURGE DURING TRAFFIC SPIKES MIGHT CAUSE SUDDEN STRESS TO SERVER RESOURCES ## ## KEEP IN MIND THAT PAGE CACHING IS DISABLED ALREADY ON DEV/STAGING WEBSITES ## ## include SlickStack configuration ## source /var/www/ss-config ## include SlickStack functions ## source /var/www/ss-functions #################################################################################################### #### SS-Purge: Message (Begin Script) ############################################################## #################################################################################################### ## this is a simple message that announces to the shell the purpose of this bash script ## ## it will only be noticed by sudo users who manually call ss core bash scripts ## ## echo message ## echo -e "${PURPLE}Running ss-purge: Clears all caches i.e. Nginx (FastCGI), PHP OPcache, Redis (object cache), etc... ${NOCOLOR}" sleep "$SLEEP_MESSAGE_BEGIN" #################################################################################################### #### SS-Purge: Clear Cache (PHP OPcache) ########################################################### #################################################################################################### ## by default OPcache only caches PHP files for a few seconds but we clear it to be sure ## ## this snippet will also clear physical OPcache files if the feature is enabled ## ## run ss-purge-opcache ## source /var/www/ss-purge-opcache #################################################################################################### #### SS-Purge: Clear Cache (WordPress Transients) ################################################## #################################################################################################### ## manually deletes all transients (temporary query cache data) from WordPress database ## ## this is a more forceful approach than simply purging the Redis object cache ## if [[ "$SS_APP" == "wordpress" || -z "$SS_APP" ]]; then ## run ss-purge-transients ## source /var/www/ss-purge-transients fi #################################################################################################### #### SS-Purge: Clear Cache (Redis Server) ########################################################## #################################################################################################### ## when a persistent object cache is installed in WordPress it retains all WP transients ## ## therefore flushing Redis is similar to manually deleting WP transients in MySQL ## ## run ss-purge-redis ## source /var/www/ss-purge-redis #################################################################################################### ### SS-Purge: Clear Cache (Nginx FastCGI Cache) #################################################### #################################################################################################### ## here we manually delete all FastCGI cache files without touching OPcache file cache ## ## when running TMPFS this script clears the FastCGI cache files from RAM memory ## ## run ss-purge-nginx ## source /var/www/ss-purge-nginx #################################################################################################### #### SS-Purge: Touch Timestamp File (End Script) ################################################### #################################################################################################### ## this is a dummy timestamp file that will remember the last time this script was run ## ## it can be useful for developer reference and is sometimes used by SlickStack ## ## script timestamp ## touch /var/www/meta/.timestamp-ss-purge #################################################################################################### #### SlickStack: External References Used To Improve This Script (Thanks, Interwebz) ############### #################################################################################################### ## Ref: https://www.php.net/manual/en/function.opcache-reset.php#121513 ## Ref: https://stackoverflow.com/questions/5506913/bash-script-to-run-php-script ## Ref: https://coderwall.com/p/yrqrkw/delete-all-existing-wordpress-transients-in-mysql-database ## Ref: https://stackoverflow.com/questions/10422574/can-i-remove-transients-in-the-wp-options-table-of-my-wordpress-install ## Ref: https://wordpress.stackexchange.com/questions/73477/is-there-any-danger-in-deleting-all-transients ## Ref: https://stackoverflow.com/questions/20033648/how-to-run-mysql-command-on-bash ## Ref: https://serverfault.com/questions/337818/how-to-force-mysql-to-connect-by-tcp-instead-of-a-unix-socket ## Ref: https://stackoverflow.com/questions/33067909/bash-variable-under-a-mysql-query ## Ref: https://dev.mysql.com/doc/refman/5.7/en/examples.html ## Ref: https://stackoverflow.com/questions/25044817/zend-opcache-opcache-enable-cli-1-or-0-what-does-it-do#comment91052089_35880017 ## Ref: https://codex.wordpress.org/Class_Reference/WP_Object_Cache ## Ref: https://pressidium.com/blog/2017/wordpress-object-caching-redis-memcached-and-native-apis/ ## Ref: https://pressjitsu.com/blog/transient-cache-alternatives/ ## Ref: https://unix.stackexchange.com/questions/87258/delete-all-files-except-in-a-certain-subdirectory-with-find ## Ref: https://github.com/littlebizzy/slickstack/issues/57 ## SS_EOF