diff --git a/DynareMain.cc b/DynareMain.cc
index 7fed1e7858964a73f3421dc7d5664b2fa1288f6b..19d773b8bec0001fb8f6b714f07ba4f0e709b68f 100644
--- a/DynareMain.cc
+++ b/DynareMain.cc
@@ -35,7 +35,8 @@
    Splitting main() in two parts was necessary because ParsingDriver.h and MacroDriver.h can't be
    included simultaneously (because of Bison limitations).
 */
-void main2(stringstream &in, string &basename, bool debug, bool clear_all, bool no_tmp_terms, bool no_log, bool no_warn, bool warn_uninit, bool console, bool nograph, bool nointeractive, 
+void main2(stringstream &in, string &basename, bool debug, bool clear_all, bool clear_global, bool no_tmp_terms, bool no_log, bool no_warn,
+           bool warn_uninit, bool console, bool nograph, bool nointeractive,
            bool parallel, const string &parallel_config_file, const string &cluster_name, bool parallel_slave_open_mode,
            bool parallel_test, bool nostrict, FileOutputType output_mode, LanguageOutputType lang
 #if defined(_WIN32) || defined(__CYGWIN32__)
@@ -46,7 +47,7 @@ void main2(stringstream &in, string &basename, bool debug, bool clear_all, bool
 void
 usage()
 {
-  cerr << "Dynare usage: dynare mod_file [debug] [noclearall] [savemacro[=macro_file]] [onlymacro] [nolinemacro] [notmpterms] [nolog] [warn_uninit]"
+  cerr << "Dynare usage: dynare mod_file [debug] [noclearall] [onlyclearglobals] [savemacro[=macro_file]] [onlymacro] [nolinemacro] [notmpterms] [nolog] [warn_uninit]"
        << " [console] [nograph] [nointeractive] [parallel[=cluster_name]] [conffile=parallel_config_path_and_filename] [parallel_slave_open_mode] [parallel_test] "
        << " [-D<variable>[=<value>]] [nostrict] [output=dynamic|first|second|third] [language=C|C++]"
 #if defined(_WIN32) || defined(__CYGWIN32__)
@@ -73,6 +74,7 @@ main(int argc, char **argv)
     }
 
   bool clear_all = true;
+  bool clear_global = false;
   bool save_macro = false;
   string save_macro_file;
   bool debug = false;
@@ -106,6 +108,11 @@ main(int argc, char **argv)
         debug = true;
       else if (!strcmp(argv[arg], "noclearall"))
         clear_all = false;
+      else if (!strcmp(argv[arg], "onlyclearglobals"))
+        {
+          clear_all = false;
+          clear_global = true;
+        }
       else if (!strcmp(argv[arg], "onlymacro"))
         only_macro = true;
       else if (strlen(argv[arg]) >= 9 && !strncmp(argv[arg], "savemacro", 9))
@@ -277,7 +284,7 @@ main(int argc, char **argv)
     return EXIT_SUCCESS;
 
   // Do the rest
-  main2(macro_output, basename, debug, clear_all, no_tmp_terms, no_log, no_warn, warn_uninit, console, nograph, nointeractive, 
+  main2(macro_output, basename, debug, clear_all, clear_global, no_tmp_terms, no_log, no_warn, warn_uninit, console, nograph, nointeractive,
         parallel, parallel_config_file, cluster_name, parallel_slave_open_mode, parallel_test, nostrict, output_mode, language
 #if defined(_WIN32) || defined(__CYGWIN32__)
         , cygwin, msvc
diff --git a/DynareMain2.cc b/DynareMain2.cc
index 8f82a059f81940be169ac5bc731312dc0f6f6590..376dd20a90a8e82efc16a3f681164285058be9cd 100644
--- a/DynareMain2.cc
+++ b/DynareMain2.cc
@@ -25,7 +25,7 @@
 #include "ExtendedPreprocessorTypes.hh"
 
 void
-main2(stringstream &in, string &basename, bool debug, bool clear_all, bool no_tmp_terms, bool no_log, bool no_warn, bool warn_uninit, bool console, bool nograph, bool nointeractive,
+main2(stringstream &in, string &basename, bool debug, bool clear_all, bool clear_global, bool no_tmp_terms, bool no_log, bool no_warn, bool warn_uninit, bool console, bool nograph, bool nointeractive,
       bool parallel, const string &parallel_config_file, const string &cluster_name, bool parallel_slave_open_mode,
       bool parallel_test, bool nostrict, FileOutputType output_mode, LanguageOutputType language
 #if defined(_WIN32) || defined(__CYGWIN32__)
@@ -60,7 +60,7 @@ main2(stringstream &in, string &basename, bool debug, bool clear_all, bool no_tm
   if (output_mode != none)
     mod_file->writeExternalFiles(basename, output_mode, language);
   else
-    mod_file->writeOutputFiles(basename, clear_all, no_log, no_warn, console, nograph, nointeractive, config_file
+    mod_file->writeOutputFiles(basename, clear_all, clear_global, no_log, no_warn, console, nograph, nointeractive, config_file
 #if defined(_WIN32) || defined(__CYGWIN32__)
                              , cygwin, msvc
 #endif
diff --git a/ModFile.cc b/ModFile.cc
index 667e7ac634da512f77b164c2b98a4e65659b006f..809255973de8f69766798ce2104112b563f1209f 100644
--- a/ModFile.cc
+++ b/ModFile.cc
@@ -522,7 +522,7 @@ ModFile::computingPass(bool no_tmp_terms, FileOutputType output)
 }
 
 void
-ModFile::writeOutputFiles(const string &basename, bool clear_all, bool no_log, bool no_warn, bool console, bool nograph, bool nointeractive, const ConfigFile &config_file
+ModFile::writeOutputFiles(const string &basename, bool clear_all, bool clear_global, bool no_log, bool no_warn, bool console, bool nograph, bool nointeractive, const ConfigFile &config_file
 #if defined(_WIN32) || defined(__CYGWIN32__)
                           , bool cygwin, bool msvc
 #endif
@@ -559,6 +559,8 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool no_log, b
 
   if (clear_all)
     mOutputFile << "clear all" << endl;
+  else if (clear_global)
+    mOutputFile << "clear M_ options_ oo_ estim_params_ bayestopt_ dataset_;" << endl;
 
   mOutputFile << "tic;" << endl
 	      << "% Save empty dates and dseries objects in memory." << endl
diff --git a/ModFile.hh b/ModFile.hh
index d8b042a73ed55c6749140b7ec603b9e407decec8..04aa1b5e4ac1451a2a075ee2492c3339e94116c8 100644
--- a/ModFile.hh
+++ b/ModFile.hh
@@ -136,7 +136,7 @@ public:
     \param cygwin Should the MEX command of use_dll be adapted for Cygwin?
     \param msvc Should the MEX command of use_dll be adapted for MSVC?
   */
-  void writeOutputFiles(const string &basename, bool clear_all, bool no_log, bool no_warn, bool console, bool nograph, bool nointeractive, const ConfigFile &config_file
+  void writeOutputFiles(const string &basename, bool clear_all, bool clear_global, bool no_log, bool no_warn, bool console, bool nograph, bool nointeractive, const ConfigFile &config_file
 #if defined(_WIN32) || defined(__CYGWIN32__)
                         , bool cygwin, bool msvc
 #endif