Skip to content

Add automatic cleanup job for orphaned topic counters (total_projects_count)

Problem

Topics can show incorrect project counts when projects are deleted but ProjectTopic records remain orphaned, causing topic.total_projects_count to be out of sync.

Current Behavior

  • Topic shows "X projects" in counter:

    image.png

  • Clicking on topic shows empty site:

    image.png

    (potentially also fewer projects than counter indicates)

Validated discrepancy:

image.png

Expected Behavior

  • Topic counters should accurately reflect the number of visible projects
  • Automatic cleanup should handle orphaned ProjectTopic records

Proposed Solution

Add a scheduled cleanup job (similar to existing cleanup workers) that:

  1. Identifies orphaned ProjectTopic records
  2. Runs Projects::Topic.reset_counters for affected topics (topics where topic.total_projects_count != topic.projects.count )
  3. Cleans up orphaned records

Related Code

  • Projects::ProjectTopic model with counter_cache: :total_projects_count
  • Similar pattern exists in Packages::Cleanup::DeleteOrphanedDependenciesWorker

Impact

  • Improves data consistency
  • Better user experience
  • Reduces admin maintenance overhead
Edited by 🤖 GitLab Bot 🤖