# Rebake all posts matching a pattern

**URL:** https://meta.discourse.org/t/rebake-all-posts-matching-a-pattern/48713
**Category:** Self-Hosting
**Tags:** rails-console, how-to
**Created:** [August 16, 2016, 8:01pm UTC](https://meta.discourse.org/t/rebake-all-posts-matching-a-pattern/48713 "2016-08-16T20:01:30Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Discourse](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/discourse/32/148734_2.png) [@Discourse](https://meta.discourse.org/u/Discourse)
#### Post date: [August 16, 2016, 8:01pm UTC](https://meta.discourse.org/t/rebake-all-posts-matching-a-pattern/48713/1 "2016-08-16T20:01:30Z")

</div>

> 🔖 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:

```bash
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.

```bash
rake posts:rebake_match["pattern"]

```

For example, to rebake all posts containing `:slight_smile:` in their raw content:

```bash
rake posts:rebake_match[":slight_smile:"]

```

## Rebake all posts matching a regular expression

> ℹ PostgreSQL uses [POSIX Regular Expressions](https://www.postgresql.org/docs/current/functions-matching.html#FUNCTIONS-POSIX-REGEXP) to perform regex matching. The matching will be case-insensitive. A newline-sensitive flag (`(?n)`) is automatically applied, which means `.` will not match newline characters and `^`/`$` will match at line boundaries.

For regular expressions, use this command.

```bash
rake posts:rebake_match["pattern",regex]

```

Examples:

- Rebake posts containing `:slight_smile:` or `Discourse`:

- Rebake posts starting with `Today`:

- Rebake posts starting with `Today` or `Yesterday`:

## 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:

```bash
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:

```bash
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:

```ruby
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

```ruby
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

```ruby
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:

---

<div class="post-metadata">

### Author: ![Isambard](https://avatars.discourse-cdn.com/v4/letter/i/858c86/32.png) [@Isambard](https://meta.discourse.org/u/Isambard)
#### Post date: [May 30, 2024, 11:18pm UTC](https://meta.discourse.org/t/rebake-all-posts-matching-a-pattern/48713/21 "2024-05-30T23:18:42Z")

</div>

Is there an easy way to uncook all posts/threads that were created before a specified date?
