diff --git a/src/caoscrawler/scanner.py b/src/caoscrawler/scanner.py
index 5bd662d3fb8efd77564066eae353a17c499d62e8..6f5545b1d0d53114282a3477b6855900f5294520 100644
--- a/src/caoscrawler/scanner.py
+++ b/src/caoscrawler/scanner.py
@@ -326,17 +326,6 @@ def scanner(items: list[StructureElement],
     for record in scoped_records:
         crawled_data.append(record)
 
-    # TODO: the scoped variables should be cleaned up as soon if the variables
-    #       are no longer in the current scope. This can be implemented as follows,
-    #       but this breaks the test "test_record_structure_generation", because
-    #       some debug info is also deleted. This implementation can be used as soon
-    #       as the remaining problems with the debug_tree are fixed.
-    # Delete the variables that are no longer needed:
-    # scoped_names = record_store.get_names_current_scope()
-    # for name in scoped_names:
-    #     del record_store[name]
-    #     del general_store[name]
-
     return crawled_data
 
 
@@ -355,10 +344,18 @@ def scan_directory(dirname: str, crawler_definition_path: str,
     Convenience function that starts the crawler (calls start_crawling)
     with a single directory as the StructureElement.
 
+    Parameters
+    ----------
+
     restricted_path: optional, list of strings
             Traverse the data tree only along the given path. When the end of the given path
             is reached, traverse the full tree as normal. See docstring of 'scanner' for
             more details.
+
+    Returns
+    -------
+    crawled_data : list
+        the final list with the target state of Records.
     """
 
     crawler_definition = load_definition(crawler_definition_path)
diff --git a/unittests/cfood_variable_deletion.yml b/unittests/cfood_variable_deletion.yml
new file mode 100644
index 0000000000000000000000000000000000000000..9edfc1b06cdd6f57a52cc71a96306984ee9f2dbe
--- /dev/null
+++ b/unittests/cfood_variable_deletion.yml
@@ -0,0 +1,29 @@
+
+Data:
+  type: Directory
+  match: (.*)
+  subtree:
+    Data_1:
+      type: Directory
+      match: ^Data_1$
+      subtree:
+        Subdir:
+          type: Directory
+          match: ^(?P<test_1>.*)$
+          records:
+            DummyRecord:
+              name: "Record from Data_1"
+              var1: $test_1
+              var2: $test_2
+    Data_2:
+      type: Directory
+      match: ^Data_2$
+      subtree:
+        Subdir:
+          type: Directory
+          match: ^(?P<test_2>.*)$
+          records:
+            DummyRecord:
+              name: "Record from Data_2"
+              var1: $test_1
+              var2: $test_2
diff --git a/unittests/cfood_variable_deletion2.yml b/unittests/cfood_variable_deletion2.yml
new file mode 100644
index 0000000000000000000000000000000000000000..729fe519e00323c046d77e93904421c3ba6a666e
--- /dev/null
+++ b/unittests/cfood_variable_deletion2.yml
@@ -0,0 +1,29 @@
+
+Data:
+  type: Directory
+  match: (?P<test_1>.*)
+  subtree:
+    Data_1:
+      type: Directory
+      match: ^Data_1$
+      subtree:
+        Subdir:
+          type: Directory
+          match: ^(?P<test_1>.*)$
+          records:
+            DummyRecord:
+              name: "Record from Data_1"
+              var1: $test_1
+              var2: $test_2
+    Data_2:
+      type: Directory
+      match: ^Data_2$
+      subtree:
+        Subdir:
+          type: Directory
+          match: ^(?P<test_2>.*)$
+          records:
+            DummyRecord:
+              name: "Record from Data_2"
+              var1: $test_1
+              var2: $test_2
diff --git a/unittests/test_directories/example_variable_deletion/Data_1/bla/README.md b/unittests/test_directories/example_variable_deletion/Data_1/bla/README.md
new file mode 100644
index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
diff --git a/unittests/test_directories/example_variable_deletion/Data_2/test/README.md b/unittests/test_directories/example_variable_deletion/Data_2/test/README.md
new file mode 100644
index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
diff --git a/unittests/test_scanner.py b/unittests/test_scanner.py
index 6aa7287b2449f49706cb352d24fa8abf66f87115..1a1c8c1823aee5e84d35b9ddaf7dc919b56e40dc 100644
--- a/unittests/test_scanner.py
+++ b/unittests/test_scanner.py
@@ -245,3 +245,34 @@ def test_record_generation():
     persons_found = check_properties(persons, check_props)
     for f in persons_found:
         assert f > 0
+
+
+def test_variable_deletion_problems():
+    records = scan_directory(UNITTESTDIR / "test_directories" / "example_variable_deletion",
+                             UNITTESTDIR / "cfood_variable_deletion.yml")
+
+    for record in records:
+        if record.name == "Record from Data_1":
+            assert record.get_property("var1").value == "bla"
+            assert record.get_property("var2").value == "$test_2"
+        elif record.name == "Record from Data_2":
+            assert record.get_property("var1").value == "$test_1"
+            assert record.get_property("var2").value == "test"
+        else:
+            raise RuntimeError("Wrong name")
+
+    records = scan_directory(UNITTESTDIR / "test_directories" / "example_variable_deletion",
+                             UNITTESTDIR / "cfood_variable_deletion2.yml")
+
+    # For the following test the order of records is actually important:
+    assert records[0].name == "Record from Data_1"
+    assert records[1].name == "Record from Data_2"
+    for record in records:
+        if record.name == "Record from Data_1":
+            assert record.get_property("var1").value == "bla"
+            assert record.get_property("var2").value == "$test_2"
+        elif record.name == "Record from Data_2":
+            assert record.get_property("var1").value == "example_variable_deletion"
+            assert record.get_property("var2").value == "test"
+        else:
+            raise RuntimeError("Wrong name")