diff --git a/Source/cmExportFileGenerator.cxx b/Source/cmExportFileGenerator.cxx
index 2b3ce148c496e2df11a40c0ecc08546dcc58ae10..89a8f6c78f7b526f46accc8ae22c465a35da9811 100644
--- a/Source/cmExportFileGenerator.cxx
+++ b/Source/cmExportFileGenerator.cxx
@@ -1040,6 +1040,9 @@ void cmExportFileGenerator::GenerateExpectedTargetsCode(std::ostream& os,
         "  endif()\n"
         "endforeach()\n"
         "if(\"${_targetsDefined}\" STREQUAL \"${_expectedTargets}\")\n"
+        "  unset(_targetsDefined)\n"
+        "  unset(_targetsNotDefined)\n"
+        "  unset(_expectedTargets)\n"
         "  set(CMAKE_IMPORT_FILE_VERSION)\n"
         "  cmake_policy(POP)\n"
         "  return()\n"