Skip to content

Commit 2cc42ca

Browse files
authored
docs: add guide on clearing cache (#14133)
* docs: add guide on clearing cache * update llms
1 parent e899013 commit 2cc42ca

File tree

6 files changed

+95
-4
lines changed

6 files changed

+95
-4
lines changed

www/apps/book/public/llms-full.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43027,11 +43027,11 @@ In this guide, you'll learn how to clear cached data in Medusa.
4302743027

4302843028
## Why Clear Cache?
4302943029

43030-
You should mainly cache data that isn't frequently changing, such as product details or categories. However, there are scenarios where you might need to clear the cache.
43030+
You should mainly cache data that isn't frequently changing, such as product details or categories. However, there are scenarios where you might need to clear the cache of that data manually.
4303143031

43032-
For example, if you've integrated a third-party system that updates product information outside of Medusa, or if you've cached data from the third-party system, Medusa won't be aware of these changes.
43032+
For example, if you've integrated a third-party system that updates product information outside of Medusa, or if you've cached data from the third-party system, Medusa won't be aware of changes made externally.
4303343033

43034-
In such cases, clearing the cache ensures that Medusa fetches the most up-to-date information from the source rather than relying on outdated cached data.
43034+
In such cases, you should clear the cache in Medusa to ensure it fetches the most up-to-date information from the source rather than relying on outdated cached data.
4303543035

4303643036
***
4303743037

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
export const metadata = {
2+
title: `How to Clear Cached Data`,
3+
}
4+
5+
# {metadata.title}
6+
7+
In this guide, you'll learn how to clear cached data in Medusa.
8+
9+
## Why Clear Cache?
10+
11+
You should mainly cache data that isn't frequently changing, such as product details or categories. However, there are scenarios where you might need to clear the cache of that data manually.
12+
13+
For example, if you've integrated a third-party system that updates product information outside of Medusa, or if you've cached data from the third-party system, Medusa won't be aware of changes made externally.
14+
15+
In such cases, you should clear the cache in Medusa to ensure it fetches the most up-to-date information from the source rather than relying on outdated cached data.
16+
17+
---
18+
19+
## How to Clear Cache
20+
21+
This section explains how to clear data cached by the Caching Module in Medusa.
22+
23+
### Identify Cache Tags
24+
25+
Before clearing the cache, identify the specific cache tags associated with the data you want to clear.
26+
27+
When you cache entities with the [Query](!docs!/learn/fundamentals/module-links/query) or [Index Module](!docs!/learn/fundamentals/module-links/index-module), the Caching Module automatically generates tags based on the entity type and its ID:
28+
29+
- `Entity:id`: Cache tag for a single record of an entity. For example, `Product:prod_123` caches a single product with the ID `prod_123`.
30+
- `Entity:list:*`: Cache tag for a list of records of an entity. For example, `Product:list:*` caches a list of products.
31+
32+
To clear the cache for a specific product, use the `Product:{id}` tag. To clear the cache for all products, use the `Product:list:*` tag.
33+
34+
Refer to the [Caching Module Concepts guide](../../concepts/page.mdx#caching-tags-convention) to learn more about cache tags.
35+
36+
### Clear Cache with Caching Module Service
37+
38+
To clear cached data, use the [clear](/references/caching-service#clear) method of the Caching Module's service. You can resolve the service in a workflow step, API route, subscriber, or scheduled job, then call the `clear` method with the identified cache tags.
39+
40+
For example, to clear the cache for specific products in a workflow step:
41+
42+
```ts
43+
import { createStep, StepResponse } from "@medusajs/framework/workflows-sdk"
44+
import { Modules } from "@medusajs/framework/utils"
45+
46+
type ClearProductCacheInput = {
47+
productId: string | string[]
48+
}
49+
50+
export const clearProductCacheStep = createStep(
51+
"clear-product-cache",
52+
async ({ productId }: ClearProductCacheInput, { container }) => {
53+
const cachingModuleService = container.resolve(Modules.CACHING)
54+
55+
const productIds = Array.isArray(productId) ? productId : [productId]
56+
57+
// Clear cache for all specified products
58+
for (const id of productIds) {
59+
if (id) {
60+
await cachingModuleService.clear({
61+
tags: [`Product:${id}`],
62+
})
63+
}
64+
}
65+
66+
return new StepResponse({})
67+
}
68+
)
69+
```
70+
71+
In this example, the `clearProductCacheStep` step takes a `productId` (or an array of IDs) as input and clears the cache for each specified product using its cache tag.
72+
73+
You can then use this step in a workflow to clear the cache whenever necessary, such as after receiving a webhook from a third-party system indicating that product data has changed.

www/apps/resources/generated/edit-dates.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6718,5 +6718,6 @@ export const generatedEditDates = {
67186718
"references/core_flows/Product/Workflows_Product/functions/core_flows.Product.Workflows_Product.batchImageVariantsWorkflow/page.mdx": "2025-11-05T12:22:20.639Z",
67196719
"references/core_flows/Product/Workflows_Product/functions/core_flows.Product.Workflows_Product.batchVariantImagesWorkflow/page.mdx": "2025-11-05T12:22:20.671Z",
67206720
"app/storefront-development/guides/react-native-expo/page.mdx": "2025-11-06T07:18:45.347Z",
6721-
"app/how-to-tutorials/tutorials/customer-tiers/page.mdx": "2025-11-25T08:24:24.566Z"
6721+
"app/how-to-tutorials/tutorials/customer-tiers/page.mdx": "2025-11-25T08:24:24.566Z",
6722+
"app/infrastructure-modules/caching/guides/clear-cache/page.mdx": "2025-11-26T13:19:26.629Z"
67226723
}

www/apps/resources/generated/files-map.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,10 @@ export const filesMap = [
847847
"filePath": "/www/apps/resources/app/infrastructure-modules/caching/concepts/page.mdx",
848848
"pathname": "/infrastructure-modules/caching/concepts"
849849
},
850+
{
851+
"filePath": "/www/apps/resources/app/infrastructure-modules/caching/guides/clear-cache/page.mdx",
852+
"pathname": "/infrastructure-modules/caching/guides/clear-cache"
853+
},
850854
{
851855
"filePath": "/www/apps/resources/app/infrastructure-modules/caching/guides/memcached/page.mdx",
852856
"pathname": "/infrastructure-modules/caching/guides/memcached"

www/apps/resources/generated/generated-infrastructure-modules-sidebar.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,14 @@ const generatedgeneratedInfrastructureModulesSidebarSidebar = {
148148
"type": "sub-category",
149149
"title": "Guides",
150150
"children": [
151+
{
152+
"loaded": true,
153+
"isPathHref": true,
154+
"type": "link",
155+
"path": "/infrastructure-modules/caching/guides/clear-cache",
156+
"title": "Clear Cache",
157+
"children": []
158+
},
151159
{
152160
"loaded": true,
153161
"isPathHref": true,

www/apps/resources/sidebars/infrastructure-modules.mjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ export const infrastructureModulesSidebar = [
9898
type: "sub-category",
9999
title: "Guides",
100100
children: [
101+
{
102+
type: "link",
103+
path: "/infrastructure-modules/caching/guides/clear-cache",
104+
title: "Clear Cache",
105+
},
101106
{
102107
type: "link",
103108
path: "/references/caching-module-provider",

0 commit comments

Comments
 (0)