LOG IN SIGN UP
Documentation

Wildcard purges

Wildcard purging allows you to flush the cache of all pages under a directory branch or URL path; for example, you want to empty the cache of all pages under your "/service" path. Having to purge each URL one by one using the Fastly API or via the Fastly app is not very efficient.

Although Fastly does not have a specific wildcard purge function, you can implement the same behavior by making a small configuration change using surrogate keys. Surrogate keys allow you to tag a group of objects with a keyword (key) and then purge multiple pieces of content at once with it through our Fastly application or API.

To purge content based on wildcard paths, follow the steps below.

Create a default wildcard header

We set a default wildcard so that we have the flexibility to append other surrogate keys to a URL path.

  1. Log in to the Fastly application.
  2. Click the configure tab to access the control panel.

    The configure tab

  3. Select the appropriate service from the Service menu.

  4. Click the blue Configure button to the right of the service name.

  5. Click the Content section.

  6. In the Headers area, click the New button to create a new header. The New Header window appears.

    settings for creating a default wildcard header

  7. Fill out the New Header window as follows:

    • In the Name field, type Default Wildcard.
    • From the Type/Action menus, select Cache and Set.
    • In the Destination field type http.Surrogate-Key.
    • In the Source field type "".
    • From the Ignore if Set menu, select No.
    • In the Priority field, type 10.
  8. Click the Create button. A new header appears in the Headers area of the Content section.

Create headers for each wildcard path being purged

Next, create a header for each of the wildcard paths you need the ability to purge. For instance, you want to purge the wildcard path /*/foo.

  1. Click the New button again to create another new header.

    a wildcard header for the path being purged

  2. Fill out the next New Header window as follows:

    • In the Name field, type /*/foo Wildcard.
    • From the Type/Action menus, select Cache and Append.
    • In the Destination field type http.Surrogate-Key.
    • In the Source field type " */foo".
    • From the Ignore if Set menu, select No.
    • In the Priority field, type 20.
  3. Click the Create button. A new header appears in the Headers area of the Content section.

Notice the Action is set to Append to add to the default wildcard surrogate key. Also, there is a space before the asterisk in the Source field, which is important when appending multiple surrogate keys to a URL. Finally, the Priority is set to 20 so that the Default Wildcard header is executed first and then the wildcard path appends.

Create conditions for each wildcard path being purged

Finally, create a condition for each of the wildcard paths you need the ability to purge. Once you add the new wildcard path header, a gear icon appears to the right of its name in the Header area of the Content section.

This is where you'll create a new condition following the steps below.

  1. Click the gear icon to the right of the wildcard path header name and select Cache Conditions from the menu. The Choose Condition window appears.

    the choose condition window

  2. In the Options section, click the New button to add a new condition. The New Condition window appears.

    the new condition window

  3. Fill out the New Condition controls as follows:

    • In the Name field, type /*/foo Wildcard Condition.
    • In the Apply If field, type req.url ~ "^/[^/]*/foo$".
    • In the Priority field, type 10.
  4. Click Create to create the new condition.

What does the condition mean? In the Apply If field above, the first "^" and "$" tells Fastly to look for the following pattern:

Some examples would be /a/foo, /bar/foo, and /c/foo. You could also remove the first "^" and ">"$" to allow the condition to be more general so that the pattern can occur in the middle of a URL path.

Some other examples for URL wildcard conditions:

Apply If field Matched pattern
req.url ~ "/[^/]*/foo" /delta/wow/a/foo/neat/cool/img.gif
req.url ~ "^/.*/foo$" /a/b/c/d/e/f/foo

Purge the wildcard

Ready to purge that wildcard? You can do this through the UI using the steps below.

  1. Select the appropriate service from the Service menu.
  2. Click the Purge button to the right of the service name and then select Purge Key from the menu that appears.

    the purge drop down

    The Purge Key window appears.

  3. In the Key field, type the Source name of the surrogate key you want to purge, without the quotations.

    the purge key window

    Continuing with our example, you would type */foo without the quotes that were entered in the Source field of the New Header window above.

  4. Click the Purge Key button.

You can also use our key-based purging via the API using an HTTP request like so:

POST /service/<Fastly Service ID>/purge/*/foo
Fastly-Key: <Fastly Key>

Both the UI version and the API version will purge any content that was associated with the "*/foo" surrogate key according to the setup in your header rules.