From 3815286acbc0d02b7fcb88cc930ee5474980b635 Mon Sep 17 00:00:00 2001 From: Muhammad Arslan Abdul Rauf Date: Tue, 16 Dec 2025 17:35:19 +0500 Subject: [PATCH 1/2] fix: try deleting the XBlock from draft-branch if not in published --- xmodule/modulestore/split_mongo/split.py | 34 +++++++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/xmodule/modulestore/split_mongo/split.py b/xmodule/modulestore/split_mongo/split.py index 4804e9244d..aeacac2533 100644 --- a/xmodule/modulestore/split_mongo/split.py +++ b/xmodule/modulestore/split_mongo/split.py @@ -2529,10 +2529,36 @@ class SplitMongoModuleStore(SplitBulkWriteMixin, ModuleStoreWriteBase): if original_structure['root'] == block_key: raise ValueError("Cannot delete the root of a course") if block_key not in original_structure['blocks']: - raise ValueError("Cannot delete block_key {} from course {}, because that block does not exist.".format( - block_key, - usage_locator, - )) + # When user move a full sub-section to another section + # These changes are in draft-branch only and when user delete this moved + # section, we need to delete it from draft-branch + draft_course_key = usage_locator.course_key.for_branch(ModuleStoreEnum.BranchName.draft) + try: + draft_structure = self._lookup_course(draft_course_key).structure + if block_key in draft_structure['blocks']: + # Block exists in draft, use draft structure instead + original_structure = draft_structure + log.info("Block %s found in draft branch, proceeding with deletion from draft", block_key) + else: + raise ValueError( + ( + "Cannot delete block_key {} from course {}, " + "because that block does not exist in either branch." + ).format( + block_key, + usage_locator, + ) + ) + except ItemNotFoundError as exc: + raise ValueError( + ( + "Cannot delete block_key {} from course {}, " + "because that block does not exist." + ).format( + block_key, + usage_locator, + ) + ) from exc index_entry = self._get_index_if_valid(usage_locator.course_key, force) new_structure = self.version_structure(usage_locator.course_key, original_structure, user_id) new_blocks = new_structure['blocks'] From 0b7a5431643413f9f8d90b3fa00235e8d3258673 Mon Sep 17 00:00:00 2001 From: Muhammad Arslan Abdul Rauf Date: Tue, 16 Dec 2025 20:36:11 +0500 Subject: [PATCH 2/2] test: update tests related to deleting draft xblock --- xmodule/modulestore/tests/test_publish.py | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/xmodule/modulestore/tests/test_publish.py b/xmodule/modulestore/tests/test_publish.py index 545dbc250e..e07f985790 100644 --- a/xmodule/modulestore/tests/test_publish.py +++ b/xmodule/modulestore/tests/test_publish.py @@ -810,14 +810,8 @@ class ElementalDeleteItemTests(DraftPublishedOpBaseTestSetup): self.assertOLXIsDraftOnly(block_list_to_delete) # MODULESTORE_DIFFERENCE: if self.is_split_modulestore: - if revision in (ModuleStoreEnum.RevisionOption.published_only, ModuleStoreEnum.RevisionOption.all): - # Split throws an exception when trying to delete an item from the published branch - # that isn't yet published. - with pytest.raises(ValueError): - self.delete_item(block_list_to_delete, revision=revision) - else: - self.delete_item(block_list_to_delete, revision=revision) - self._check_for_item_deletion(block_list_to_delete, result) + self.delete_item(block_list_to_delete, revision=revision) + self._check_for_item_deletion(block_list_to_delete, result) else: raise Exception("Must test either Old Mongo or Split modulestore!") @@ -846,11 +840,8 @@ class ElementalDeleteItemTests(DraftPublishedOpBaseTestSetup): # The vertical is a draft. self.assertOLXIsDraftOnly(block_list_to_delete) if revision in (ModuleStoreEnum.RevisionOption.published_only, ModuleStoreEnum.RevisionOption.all): - # MODULESTORE_DIFFERENCE: - # Split throws an exception when trying to delete an item from the published branch - # that isn't yet published. - with pytest.raises(ValueError): - self.delete_item(block_list_to_delete, revision=revision) + self.delete_item(block_list_to_delete, revision=revision) + self._check_for_item_deletion(block_list_to_delete, result) else: self.delete_item(block_list_to_delete, revision=revision) self._check_for_item_deletion(block_list_to_delete, result)