This guide explains how to replace a string in all posts within a Discourse instance.
Required user level: System Administrator
Console Access Required
Want to replace a string in all the posts on a site? Let’s get started!
WARNING: We strongly recommend you take a full backup before proceeding, and make sure your string replacement is specific enough to affect only the places you want it to. If this string replacement goes wrong, every post on your site will look broken!
Access your site
Start by accessing your Discourse instance via SSH and entering the Docker container:
cd /var/discourse
./launcher enter app
Performing string replacements
Basic case-sensitive replacement
To replace a string, use the following command. Replace find
with the string to locate and replace
with the desired substitution:
rake 'posts:remap[find,replace]'
Example results:
find —> replace
Find —> Find
FIND —> FIND
finders keepers —> replaceers keepers
finding —> replaceing
This method can be useful for tasks such as replacing emojis:
rake 'posts:remap[:slightly_smiling:,:slight_smile:]'
The above command will replace all occurrences of :slightly_smiling:
with :slight_smile:
.
Case-insensitive replacement
For replacements that ignore case sensitivity use:
rake 'posts:remap[find,replace,string,true]'
Example results:
find —> replace
Find —> replace
FIND —> replace
finders keepers —> replaceers keepers
finding —> replaceing
Regex replacement
For advanced replacements with regex, format the command accordingly:
rake 'posts:remap[(?<!\\w)(?=\\w)find(?<=\\w)(?!\\w),replace,regex]'
Example results:
replace —> replace
Find —> Find
FIND —> FIND
finders keepers —> finders keepers
finding —> finding
Deleting words or strings
To completely remove a word or string, apply these commands:
Basic case-sensitive deletion
rake 'posts:delete_word[word-to-delete]'
Case-insensitive deletion
rake 'posts:delete_word[word-to-delete,string,true]'
Regex deletion
rake 'posts:delete_word[\\[color=#[0-9a-fA-F]{3,6}\\],regex]'
Last edited by @SaraDev 2024-11-14T00:39:05Z
Check document
Perform check on document: