This guide provides instructions on how to rebake all posts in Discourse that match a specific string or regular expression, utilizing the Rails console for additional advanced options.
Required user level: Administrator
Console access required
Want to rebake all the posts matching a string or regular expression? Let’s get started!
Accessing your site
To begin, connect to your Discourse Droplet via SSH and enter the Docker container for your Discourse instance:
cd /var/discourse
./launcher enter app
Rebake all posts containing a specific string
Use the following command, replacing pattern
with the string you want to match. This search is case insensitive.
rake posts:rebake_match["pattern"]
For example, to rebake all posts containing :slight_smile:
in their raw content:
rake posts:rebake_match[":slight_smile:"]
Rebake all posts matching a regular expression
PostgreSQL uses POSIX Regular Expressions to perform regex matching. The matching will be case-sensitive.
For regular expressions, use this command.
rake posts:rebake_match["pattern",regex]
Examples:
-
Rebake posts containing
:slight_smile:
orDiscourse
:rake posts:rebake_match["(:slight_smile:|Discourse)",regex]
-
Rebake posts starting with
Today
:rake posts:rebake_match["^Today",regex]
-
Rebake posts starting with
Today
orYesterday
:rake posts:rebake_match["^(Today|Yesterday)",regex]
Optional delay between rebakes
To add a 5-second delay between each rebake execution, modify the command as follows. Adjust 5
to any number of seconds you require:
rake posts:rebake_match["pattern",string,5]
Advanced: Using the Rails console
For tasks where the rake task isn’t sufficient, use the Rails console.
Accessing the Rails console
From within the Docker container, initiate the Rails console:
rails c
Using rebake uncooked rake task
Set targeted posts to ‘uncooked’ for a rebake. Exit the Rails console after running the setup, then execute the rebake command:
For rebaking a whole category:
rails c
Post.joins(:topic).where('topics.category_id = 136').update_all('baked_version = NULL')
exit
rake posts:rebake_uncooked_posts
Performing a loop in the Rails console
To perform granular operations, select posts as an array and rebake each post directly.
Posts from a particular user
user = User.find_by_username('user123')
posts = Post.where(user_id: user.id)
posts.each do |p|
p.rebake!
end
Posts containing a specific string
posts = Post.where("cooked like '%OldCDN%'")
posts.each do |p|
p.rebake!
end
Last edited by @SaraDev 2024-11-07T21:21:20Z
Check document
Perform check on document: