-
-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathss-purge-transients.txt
147 lines (110 loc) · 8.18 KB
/
ss-purge-transients.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/bash
####################################################################################################
#### author: SlickStack ############################################################################
#### link: https://slickstack.io ###################################################################
#### mirror: littlebizzy/slickstack/blob/master/bash/ss-purge-transients.txt #######################
#### path: /var/www/ss-purge-transients ############################################################
#### destination: n/a (not a boilerplate) ##########################################################
#### purpose: Manually deletes all transients data from the WordPress wp_options table #############
#### module version: MySQL 8.0.x ###################################################################
#### sourced by: ss-install ########################################################################
#### bash aliases: ss purge transients #############################################################
####################################################################################################
## PURGING CACHES DURING TRAFFIC SPIKES MIGHT CAUSE SUDDEN STRAIN TO SERVER RESOURCES ##
## KEEP IN MIND THAT MOST CACHING IS ALREADY DISABLED ON DEV/STAGING SUBDOMAINS ##
####################################################################################################
#### TABLE OF CONTENTS (SS-Purge-Transients) #######################################################
####################################################################################################
## this is a brief summary of the different code snippets you will find in this script ##
## each section should be commented so you understand what is being accomplished ##
## A. Source SS-Config + SS-Functions
## B. Touch Timestamp File
## C. Message (Begin Script)
## D. Delete Transients From Database
####################################################################################################
#### A. SS-Purge-Transients: Source SS-Config + SS-Functions #######################################
####################################################################################################
## before anything else we must source the critical variables that power this script ##
## ss-config is setup during ss-install wizard but ss-functions is hardcoded ##
## source ss-config ##
source /var/www/ss-config
## source ss-functions ##
source /var/www/ss-functions
## BELOW THIS RELIES ON SS-CONFIG AND SS-FUNCTIONS
####################################################################################################
#### B. SS-Purge-Transients: Touch Timestamp File ##################################################
####################################################################################################
## 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 ##
ss_touch "${TIMESTAMP_SS_PURGE_TRANSIENTS}"
####################################################################################################
#### C. SS-Purge-Transients: Message (Begin Script) ################################################
####################################################################################################
## this is a simple message that announces to the shell the purpose of this bash script ##
## it will only be seen by sudo users who manually run this script in the shell ##
ss_echo "${COLOR_INFO}Running ss-purge-transients... ${COLOR_RESET}"
####################################################################################################
#### D. SS-Purge-Transients: Delete Transients From Database #######################################
####################################################################################################
## forcefully removes all transients (temporary query cache) from the WordPress database ##
## useful when object caching (Memcached) is disabled, or cached queries persist ##
## ubuntu 24.04 ##
if [[ -z "${SYSTEM_UBUNTU_VERSION}" || "${SYSTEM_UBUNTU_VERSION}" == "24.04" ]]; then
if [[ -n $(ss_mysql_user --execute "SHOW TABLES FROM ${DB_NAME} WHERE Tables_in_${DB_NAME} LIKE '${DB_PREFIX}options';") ]]; then
ss_mysql_user --execute "DELETE FROM ${DB_NAME}.${DB_PREFIX}options WHERE option_name LIKE '%_transient_%';"
fi
fi
## ubuntu 22.04 ##
if [[ "${SYSTEM_UBUNTU_VERSION}" == "22.04" ]]; then
if [[ -n $(ss_mysql_user --execute "SHOW TABLES FROM ${DB_NAME} WHERE Tables_in_${DB_NAME} LIKE '${DB_PREFIX}options';") ]]; then
ss_mysql_user --execute "DELETE FROM ${DB_NAME}.${DB_PREFIX}options WHERE option_name LIKE '%_transient_%';"
fi
fi
## ubuntu 20.04 ##
if [[ "${SYSTEM_UBUNTU_VERSION}" == "20.04" ]]; then
if [[ -n $(ss_mysql_user --execute "SHOW TABLES FROM ${DB_NAME} WHERE Tables_in_${DB_NAME} LIKE '${DB_PREFIX}options';") ]]; then
ss_mysql_user --execute "DELETE FROM ${DB_NAME}.${DB_PREFIX}options WHERE option_name LIKE '%_transient_%';"
fi
fi
## ubuntu 18.04 ##
if [[ "${SYSTEM_UBUNTU_VERSION}" == "18.04" ]]; then
if [[ -n $(ss_mysql_user --execute "SHOW TABLES FROM ${DB_NAME} WHERE Tables_in_${DB_NAME} LIKE '${DB_PREFIX}options';") ]]; then
ss_mysql_user --execute "DELETE FROM ${DB_NAME}.${DB_PREFIX}options WHERE option_name LIKE '%_transient_%';"
fi
fi
####################################################################################################
#### SlickStack: Reset Permissions (SlickStack Scripts) ############################################
####################################################################################################
## we include this permissions reset in all cron jobs and bash scripts for redundancy ##
## chmod 0700 means only the root/sudo users can execute any SlickStack scripts ##
## THIS SNIPPET DOES NOT RELY ON SS-CONFIG OR SS-FUNCTIONS
## SNIPPET: ss bash scripts, ss cron jobs
## UPDATED: 02JUL2022
chown root:root /var/www/ss* ## must be root:root
chown root:root /var/www/crons/*cron* ## must be root:root
chown root:root /var/www/crons/custom/*cron* ## must be root:root
chmod 0700 /var/www/ss* ## 0700 means only root/sudo can execute
chmod 0700 /var/www/crons/*cron* ## 0700 means only root/sudo can execute
chmod 0700 /var/www/crons/custom/*cron* ## 0700 means only root/sudo can execute
####################################################################################################
#### SlickStack: External References Used To Improve This Script (Thanks, Interwebz) ###############
####################################################################################################
## Ref: ChatGPT
## 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
## Ref: https://stackoverflow.com/questions/5609620/show-tables-statement-with-multiple-like-values
## Ref: https://wordpress.stackexchange.com/questions/383794/do-wordpress-crons-clean-up-expired-transients
## SS_EOF