Skip to content

chore(catalog): delete catalog products removed more than 90 days ago - #1632

Merged
gkhngyk merged 1 commit into
ansvisor:mainfrom
cihan-gyk:chore/catalog-removed-purge
Oct 28, 2026
Merged

gkhngyk merged 1 commit into
ansvisor:mainfrom
cihan-gyk:chore/catalog-removed-purge

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Catalog products removed from their source more than 90 days ago are deleted. A sync marks a product that left its source removed_at and never deletes it, so removed rows only grew. Ansvisor asked for this small guard on 2026-10-28. Each brand's catalog already has caps: uploads up to 4 MB, feeds up to 50 MB, 10 sources a brand. The per-plan product cap comes with the Product Visibility work.

purgeRemovedCatalogProducts() (server/src/lib/catalog/purge.js):

  • When it runs: in the daily run, right after the catalog source sweep, so it never races a sync that brings a product back.
  • What it deletes: products removed more than 90 days ago (REMOVED_KEEP_DAYS). Their variants, images and category links go with them (on delete cascade, 00278). No other table points at a catalog product.
  • What it keeps: each delete re-checks removed_at, so a product that came back between the read and the delete stays.
  • Bounds: 500 ids read per batch, 100 per delete request (the URL size chunked-in.js warns about), 10,000 products a run; the rest wait for the next night. The count is logged, and an error is logged without stopping the nightly run.

What it leaves:

  • Merchant Center item issues are left alone. They're keyed by source and replaced by each sync.
  • A shopping path's jsonb reference to a deleted product reads as gone, the same as a removed one. The server's path rebuild and price read, and the web's product cards, already handle a missing product.

sync.js's "never deleted" note now names the 90-day rule.

Related issue

None. Ansvisor's decision (2026-10-28).

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

  1. cd server && npx vitest run src/lib/catalog/purge.test.js
  2. On a dev DB, set one catalog product's removed_at to 100 days ago and another's to 10 days ago, then run the daily run: only the first is deleted, with its variants.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/) — no web change
  • yarn typecheck passes (run from web/) — no web change
  • yarn format:check passes (run from web/) — no web change
  • Changes are focused — one concern per PR

Also run in server/: npm run format, npm run lint and npm test (3,342 tests).

For you, @gkhngyk:

  • A possible bug, not touched here: sync.js's markRemoved and replaceIn put up to 500 ids in one .in() (about 19 KB of URL), which chunked-in.js says is often refused. A sync that removes more than about 100 products at once could fail there. Chunking them by 100 can be its own fix.
  • A partial index on catalog_products (removed_at) where removed_at is not null would help once catalogs are large. Not needed yet.
  • One known limit: after 90 days, a link to a product the person took out of a page's shopping path (leftOut) is no longer flagged, since its catalog row is gone. Storing the url in leftOut would keep it.

🤖 Generated with Claude Code

A sync marks a product that left its source `removed_at` and never
deleted it, so removed rows only grew. purgeRemovedCatalogProducts runs
in the daily run right after the catalog source sweep and deletes
products removed over 90 days ago (REMOVED_KEEP_DAYS); their variants,
images and category links go with them (on delete cascade, 00278). Each
delete re-checks removed_at, so a product that came back is kept.
Bounded: 500 ids read per batch, 100 per delete, 10,000 a run; errors
are logged and the nightly run goes on. Merchant Center item issues are
left to their source's sync.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, with follow-ups.

Merge state. Clean on main, and with #1633 (both touch sync.js, in different places). On the tree with both, the catalog and shopping-path tests pass (161).

Checked

  • What a deleted product takes with it. Only three tables point at catalog_products: variants, images and category links, all on delete cascade (00278), and each has an index on product_id, so a delete of 100 products stays an index walk. Images that hang off a deleted variant go too (catalog_images.variant_id, cascade, indexed). Nothing else holds a foreign key to a product: Merchant Center issues (00296) are keyed by source and offer id, category pages (00350) by category name, and the conversion tables (00287, 00288) by page. No table holds a trigger on the catalog's rows.
  • Shopping paths. A product id in content_drafts.shopping_path with no row reads as gone in the API's rebuild (keptProducts, with the title from the stored path) and in the editor's check (getShoppingCheck). A product grid's refresh (getProductsForBlock) leaves a missing product out, the same as a removed one.
  • A sync that brings a product back. Each delete asks again for removed_at < cutoff, so a product a sync made live after the read stays. A sync that read the old row before the delete still works: it writes products by upsert on (brand, source, external id), so the product comes back as a new row, and its variants, read after that write, come back under the new id. The test for this case fails when the delete's removed_at filter is dropped.
  • Bounds. 500 ids read a batch, 100 a delete request, 10,000 a night. The loop stops on a short read or a batch that deleted nothing, so it can't spin.
  • Daily run. The purge runs after the source sweep and never throws, so a failure costs only that night's purge.

Follow-ups

  1. Removed variants of live products are kept forever. A product that's still sold but dropped a size or colour keeps that variant row, with removed_at set, for good. The same rule for catalog_variants (removed over 90 days ago, product live) would close it. Their images go with them by cascade.
  2. Categories with no products left. catalog_categories rows whose products were all deleted stay. catalog_category_counts doesn't count them (inner join), but they still sit in the table. Deleting categories with no product link left after the purge would keep it tidy.
  3. A test for the daily run's order. Nothing checks that runDailyTracking calls the purge after the sweep, or that a failed sweep still lets the purge run. A small test on that chain would stop someone from making the two run side by side.
  4. Index once catalogs grow (as the description says). The nightly read is removed_at < cutoff limit 500 with no index on removed_at, so it scans the table each night even when nothing is old enough. A partial index on (removed_at) where removed_at is not null makes it cheap.

Before the API deploy. Nothing changes. The purge lives in the API's daily run. Since the catalog is newer than 90 days, the first products it can delete are those removed 90 days after the first sync.

@gkhngyk
gkhngyk merged commit 944bca4 into ansvisor:main Oct 28, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants