diff --git a/mysql-test/include/maria_empty_logs.inc b/mysql-test/include/maria_empty_logs.inc
index f1835c0d2c3..bdbe3ff3ac7 100644
--- a/mysql-test/include/maria_empty_logs.inc
+++ b/mysql-test/include/maria_empty_logs.inc
@@ -11,6 +11,7 @@
 connection default;
 let $default_db=`select database()`;
 let $MYSQLD_DATADIR= `SELECT @@datadir`;
+let $pid_file=`select @@pid_file`;
 
 #it will used at end of test for wait_for_status_var.inc primitive
 #let $status_var= Threads_connected;
@@ -23,6 +24,7 @@ wait-maria_empty_logs.inc
 EOF
 
 --source include/mysqladmin_shutdown.inc
+--source include/wait_until_no_pidfile.inc
 
 --disable_warnings
 if (!$mel_keep_control_file)
diff --git a/mysql-test/include/wait_until_no_pidfile.inc b/mysql-test/include/wait_until_no_pidfile.inc
new file mode 100644
index 00000000000..9448400522b
--- /dev/null
+++ b/mysql-test/include/wait_until_no_pidfile.inc
@@ -0,0 +1,30 @@
+# Include this script after a shutdown to wait until the pid file,
+# stored in $pid_file, has disappered.
+
+#--echo $pid_file
+
+--disable_result_log
+--disable_query_log
+# Wait one minute
+let $counter= 600;
+while ($counter)
+{
+--error 0,1
+--file_exists $pid_file
+  if (!$errno)
+  {
+    dec $counter;
+    --real_sleep 0.1
+  }
+  if ($errno)
+  {
+    let $counter= 0;
+  }
+}
+if (!$errno)
+{
+  --die Pid file "$pid_file" failed to disappear
+}
+
+--enable_query_log
+--enable_result_log
diff --git a/storage/maria/ma_loghandler.c b/storage/maria/ma_loghandler.c
index 0b403fd456d..857bb59d851 100644
--- a/storage/maria/ma_loghandler.c
+++ b/storage/maria/ma_loghandler.c
@@ -8427,34 +8427,41 @@ my_bool translog_is_file(uint file_no)
 
 static uint32 translog_first_file(TRANSLOG_ADDRESS horizon, int is_protected)
 {
-  uint min_file= 0, max_file;
+  uint min_file= 1, max_file;
   DBUG_ENTER("translog_first_file");
   if (!is_protected)
     mysql_mutex_lock(&log_descriptor.purger_lock);
-  if (log_descriptor.min_file_number &&
-      translog_is_file(log_descriptor.min_file_number))
+  if (log_descriptor.min_file_number)
   {
-    DBUG_PRINT("info", ("cached %lu",
-                        (ulong) log_descriptor.min_file_number));
-    if (!is_protected)
-      mysql_mutex_unlock(&log_descriptor.purger_lock);
-    DBUG_RETURN(log_descriptor.min_file_number);
+    min_file= log_descriptor.min_file_number;
+    if (translog_is_file(log_descriptor.min_file_number))
+    {
+      DBUG_PRINT("info", ("cached %lu",
+                          (ulong) log_descriptor.min_file_number));
+      if (!is_protected)
+        mysql_mutex_unlock(&log_descriptor.purger_lock);
+      DBUG_RETURN(log_descriptor.min_file_number);
+    }
   }
 
   max_file= LSN_FILE_NO(horizon);
+  if (!translog_is_file(max_file))
+  {
+    if (!is_protected)
+      mysql_mutex_unlock(&log_descriptor.purger_lock);
+    DBUG_RETURN(max_file);                      /* For compatibility */
+  }
 
   /* binary search for last file */
-  while (min_file != max_file && min_file != (max_file - 1))
+  while (min_file < max_file)
   {
     uint test= (min_file + max_file) / 2;
     DBUG_PRINT("info", ("min_file: %u  test: %u  max_file: %u",
                         min_file, test, max_file));
-    if (test == max_file)
-      test--;
     if (translog_is_file(test))
       max_file= test;
     else
-      min_file= test;
+      min_file= test+1;
   }
   log_descriptor.min_file_number= max_file;
   if (!is_protected)
@@ -8723,9 +8730,9 @@ my_bool translog_purge(TRANSLOG_ADDRESS low)
 
 /**
   @brief Purges files by stored min need file in case of
-    "ondemend" purge type
+    "one demand" purge type
 
-  @note This function do real work only if it is "ondemend" purge type
+  @note This function do real work only if it is "one demand" purge type
     and translog_purge() was called at least once and last time without
     errors
 
@@ -8764,13 +8771,14 @@ my_bool translog_purge_at_flush()
 
   min_file= translog_first_file(translog_get_horizon(), 1);
   DBUG_ASSERT(min_file != 0); /* log is already started */
-  for(i= min_file; i < log_descriptor.min_need_file && rc == 0; i++)
+  for(i= min_file; i < log_descriptor.min_need_file ; i++)
   {
     char path[FN_REFLEN], *file_name;
     DBUG_PRINT("info", ("purge file %lu\n", (ulong) i));
     file_name= translog_filename_by_fileno(i, path);
-    rc= MY_TEST(mysql_file_delete(key_file_translog,
+    rc|= MY_TEST(mysql_file_delete(key_file_translog,
                                   file_name, MYF(MY_WME)));
+    DBUG_ASSERT(rc == 0);
   }
 
   mysql_mutex_unlock(&log_descriptor.purger_lock);