Skip to content

feat: clean up content tags on deletion and diff course tag imports - #39417

Open
ufedaseyeuconsultant wants to merge 3 commits into
openedx:masterfrom
ufedaseyeuconsultant:ufedaseyeu/895-tag-cleanup-on-content-deletion-part-2
Open

ufedaseyeuconsultant wants to merge 3 commits into
openedx:masterfrom
ufedaseyeuconsultant:ufedaseyeu/895-tag-cleanup-on-content-deletion-part-2

Conversation

@ufedaseyeuconsultant

Copy link
Copy Markdown
Contributor

Description

The openedx-platform slice of openedx/openedx-core#895 (items 5 to 7 of its list): deleting content now also cleans up the content's tag associations (ObjectTag rows), and a course tag re-import applies only the differences. The openedx-core slices (the criteria-group cleanup listeners that react to these deletions) come in separate PRs on that repo.

Three changes, one commit each, all in openedx/core/djangoapps/content_tagging/:

  • Deleting a block deletes its descendants' tags (handlers.py). The existing XBLOCK_DELETED handler removes only the deleted block's own tags, because that event fires after the subtree is unreadable, so deleting a section or subsection in Studio stranded the tags of everything inside it. A new receiver on the modulestore's pre_item_delete signal walks the block's children while they are still readable and deletes each descendant's tags. The signal fires once per branch; the second firing re-walks whatever the other branch still holds, which is harmless because deleting tags is idempotent.
  • Deleting a course deletes all of its tags (handlers.py). delete_course (the management command and cms.djangoapps.contentstore.utils.delete_course) never sends XBLOCK_DELETED; it sends only the modulestore's course_deleted signal, after the course structure is gone. Nothing deleted the course's ObjectTag rows, so they were orphaned forever. A new receiver on course_deleted finds the course's tagged object ids in the ObjectTag table (the course key itself, plus ids starting with the course's block prefix, where the +type@ delimiter keeps course-v1:A+B+C from matching the blocks of course-v1:A+B+C2) and deletes them through delete_object_tags(). For a key format other than course-v1: only the exact course-key tags are deleted.
  • Course tag import applies a diff (api.py). set_exported_object_tags() clears only the taxonomies a block's tags.csv row no longer lists, then lets tag_object() apply each listed taxonomy's differences, so an unchanged re-import keeps every ObjectTag row and its id, and anything keyed to those rows stays valid. The existing rows are read from the ObjectTag model directly, because get_object_tags() hides disabled taxonomies, and the clearing runs before the apply loop so stale rows do not count toward the 100-tags-per-object limit. Free-text taxonomies are never cleared, because the export cannot represent their values, so their absence from the file says nothing. Rows whose taxonomy was deleted are removed keyed on the missing taxonomy, because their cached export id can collide with a live taxonomy's. After the rows, import_course_tags_from_csv() sweeps the tags of course blocks absent from the file: the import has already replaced the course tree, so a block the file does not list is no longer part of the course (the block itself becomes orphaned modulestore data rather than being deleted). An export with no tags.csv, or one whose tags.csv has no data rows, changes nothing: a real export always writes at least the course row, so a data-less file is treated as corrupt input rather than as "the course has no tags".

Both receivers log a warning (with the traceback) when cleanup fails and never block the deletion that triggered them; the two # pylint: disable=broad-except lines carry that intent and follow the pattern of course_date_signals/handlers.py and notifications/tasks.py.

Deleting an ObjectTag can raise ProtectedError when another app protects the row (in openedx-core 1.26.0, a CBE learner-status row does exactly that, and it is a normal outcome until openedx/openedx-core#779 turns such deletes into archiving). Each object's cleanup therefore runs in its own savepoint and a ProtectedError on one object is logged and skipped, so one protected row neither strands the cleanup of the remaining objects nor fails a course import. Every deletion goes through delete_object_tags() / tag_object() / the ObjectTag manager, so when #779 lands these paths inherit the archive behavior without change.

Who is affected

  • Course Authors: deleting a section, subsection, or unit in Studio no longer leaves tags on the deleted content's children; re-importing a course no longer churns every tag row.
  • Operators: delete_course now removes the course's tags; the log carries one info line per cleaned object plus a per-course summary line, and one warning per failed cleanup.
  • Learners: no change.

Supporting information

Testing instructions

Automated:

  1. pytest openedx/core/djangoapps/content_tagging/tests/test_handlers.py openedx/core/djangoapps/content_tagging/tests/test_api.py --ds=cms.envs.test (58 pass at this branch's head; the --ds flag matters, under LMS settings the handler tests skip as CMS-only).
  2. CI's quality checks: pylint and ruff are clean on the touched files.

New tests: in test_handlers.py, deleting a section removes the tags of its subsections, units, and components, including children of split_test and library_content blocks, while tags outside the section survive; deleting a unit removes its components' tags; a firing for an already deleted block logs a debug line and returns; deleting a course removes its course-key and block tags while a sibling course (...+C2) keeps everything; a non-course-v1: course key removes only the exact-key tags; a cleanup failure is logged as a warning; a ProtectedError on one object is skipped and the remaining objects are still cleaned. In test_api.py, re-importing a course's own export keeps every ObjectTag row with the same primary keys, including free-text rows; dropping a closed value keeps the free-text rows; a dropped tag value, a dropped taxonomy (including a disabled one), and a taxonomy-less row are removed from a listed block; a block absent from the file loses its tags, and so does a tagged course key the file does not list; a missing tags.csv and a tags.csv without data rows change no rows; a ProtectedError while clearing or sweeping is skipped and the import completes; moving a large tag set between taxonomies stays within the per-object tag limit; the non-course-v1: sweep covers only the exact key.

Manual, against a devstack with a tagged course:

  1. Tag a subsection and a component inside it, delete the parent section in Studio: both tags are gone (check the tag counts or the ObjectTag admin), tags elsewhere survive.
  2. Export the course, re-import it unchanged: tag rows keep their ids. Re-import an export edited to drop one tag: only that tag is gone.
  3. ./manage.py cms delete_course <course-id>: the course's tags are gone; the log names the count.

Deadline

None.

Other information

  • The receivers use the modulestore's legacy SignalHandler.pre_item_delete and SignalHandler.course_deleted signals because no openedx-events equivalents exist: XBLOCK_DELETED fires after the subtree is unreadable, and there is no COURSE_DELETED event.
  • "An unchanged re-import keeps every row's id" holds for rows whose taxonomy still exists; a row whose taxonomy was deleted is removed, because only the taxonomy makes its cached export id unambiguous.
  • Deleting a block on the draft branch removes its descendants' tags even while the published branch still holds those blocks, so a later "discard changes" restores the content without its tags. This matches what XBLOCK_DELETED already does for the deleted block itself; the alternative, keeping tags until the published copy also goes, would leave the tags of permanently deleted drafts behind.
  • Question for maintainers: can blocks outside the exported course tree (static tabs, the about block, orphaned blocks) carry tags? The export never lists them, so if they can be tagged, a re-import's sweep removes those tags.
  • The pre-existing get_all_object_tags() builds the same block-id prefix without the +type@ delimiter, so it can match a sibling run (...+C2); that is untouched here and is a candidate follow-up fix.
  • Moving more than 100 combined tags between two taxonomies that both stay in the file can still trip the per-object tag limit mid-import; the delete-everything-first behavior could not. The free-text rows the import now preserves count toward the same 100-tags-per-object limit, so a block whose kept free-text tags plus listed closed tags exceed it also fails to import (tag_object raises, run_imports logs it and reports the import as successful anyway, which is pre-existing run_imports behavior). Both cases need more than 100 tags on one block.

🤖 Generated with Claude Code

ufedaseyeuconsultant and others added 3 commits November 23, 2026 13:35
Deleting a section or subsection in Studio removed only the deleted
block's own ObjectTags. The XBLOCK_DELETED event that triggers tag
cleanup fires after the subtree has been removed, so its descendants
can no longer be read and their tags were left behind.

A new receiver on the modulestore pre_item_delete signal walks the
subtree before the delete and removes each descendant's tags through
the existing _delete_tags helper. The deleted block's own tags are
still removed by delete_tag_xblock. A failure during the walk is
logged as a warning and never blocks the deletion.

Part of openedx/openedx-core#895.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
The delete_course management command path sends only the legacy
SignalHandler.course_deleted signal and never sends XBLOCK_DELETED, so
the ObjectTags of a deleted course and of its blocks were orphaned.

Add a course_deleted receiver that finds the course's tagged object ids
in the ObjectTag table, since the course structure is already gone when
the signal fires. It matches the course key itself and every block key
under the course's "block-v1:" prefix, including the "+type@" delimiter
so a sibling run such as "run2" is untouched, and deletes each through
the tagging API. A cleanup failure is logged and never blocks the
deletion.

Refs openedx/openedx-core#895

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Importing a course's tags.csv deleted every ObjectTag of each listed
block and then re-created them. A re-import therefore gave every tagged
block new ObjectTag rows, which breaks anything keyed to those rows.
Once openedx/openedx-core#779 lands, it would also permanently archive
every rule with learner progress on every re-import.

The import now applies only the differences. Before the listed tags are
applied, the rows of every taxonomy the file no longer lists for a block
are cleared, including disabled taxonomies and rows whose taxonomy was
deleted. Free-text taxonomies are left alone, because the export omits
their values and their absence from the file says nothing. Each listed
taxonomy's tag_object call then adds new values and removes omitted
ones. After the rows are applied, any tagged object of the course that
is not in the file loses its tags. An object whose tags other data
protects is logged and skipped instead of failing the import. A
tags.csv with no data rows, like a missing file, changes nothing.

Refs openedx/openedx-core#895

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@openedx-webhooks openedx-webhooks added the open-source-contribution PR author is not from Axim or 2U label Nov 23, 2026
@openedx-webhooks

Copy link
Copy Markdown

Thanks for the pull request, @ufedaseyeuconsultant!

This repository is currently maintained by @openedx/wg-maintenance-openedx-platform-oncall.

Once you've gone through the following steps feel free to tag them in a comment and let them know that your changes are ready for engineering review.

🔘 Get product approval

If you haven't already, check this list to see if your contribution needs to go through the product review process.

  • If it does, you'll need to submit a product proposal for your contribution, and have it reviewed by the Product Working Group.
    • This process (including the steps you'll need to take) is documented here.
  • If it doesn't, simply proceed with the next step.
🔘 Provide context

To help your reviewers and other members of the community understand the purpose and larger context of your changes, feel free to add as much of the following information to the PR description as you can:

  • Dependencies

    This PR must be merged before / after / at the same time as ...

  • Blockers

    This PR is waiting for OEP-1234 to be accepted.

  • Timeline information

    This PR must be merged by XX date because ...

  • Partner information

    This is for a course on edx.org.

  • Supporting documentation
  • Relevant Open edX discussion forum threads
🔘 Get a green build

If one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green.

Details
Where can I find more information?

If you'd like to get more details on all aspects of the review process for open source pull requests (OSPRs), check out the following resources:

When can I expect my changes to be merged?

Our goal is to get community contributions seen and reviewed as efficiently as possible.

However, the amount of time that it takes to review and merge a PR can vary significantly based on factors such as:

  • The size and impact of the changes that it introduces
  • The need for product review
  • Maintenance status of the parent repository

💡 As a result it may take up to several weeks or months to complete a review and merge your PR.

@github-project-automation github-project-automation Bot moved this to Needs Triage in Contributions Nov 23, 2026
@ufedaseyeuconsultant ufedaseyeuconsultant changed the title feat: clean up content tags on deletion and diff course tag imports (#895) feat: clean up content tags on deletion and diff course tag imports Nov 23, 2026
@ufedaseyeuconsultant

Copy link
Copy Markdown
Contributor Author

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

open-source-contribution PR author is not from Axim or 2U

Projects

Status: Needs Triage

Development

Successfully merging this pull request may close these issues.

2 participants