Index: src/wp-includes/functions.php
===================================================================
--- src/wp-includes/functions.php	(revision 41243)
+++ src/wp-includes/functions.php	(working copy)
@@ -3303,7 +3303,7 @@
  * @access private
  */
 function _delete_option_fresh_site() {
-	update_option( 'fresh_site', 0 );
+	update_option( 'fresh_site', '0' );
 }
 
 /**
Index: tests/phpunit/tests/customize/manager.php
===================================================================
--- tests/phpunit/tests/customize/manager.php	(revision 41243)
+++ tests/phpunit/tests/customize/manager.php	(working copy)
@@ -170,18 +170,41 @@
 		$this->assertInstanceOf( 'WPDieException', $exception );
 		$this->assertContains( 'Invalid changeset UUID', $exception->getMessage() );
 
-		update_option( 'fresh_site', 0 );
+		update_option( 'fresh_site', '0' );
 		$wp_customize = new WP_Customize_Manager();
 		$wp_customize->setup_theme();
 		$this->assertFalse( has_action( 'after_setup_theme', array( $wp_customize, 'import_theme_starter_content' ) ) );
 
 		// Make sure that starter content import gets queued on a fresh site.
-		update_option( 'fresh_site', 1 );
+		update_option( 'fresh_site', '1' );
 		$wp_customize->setup_theme();
 		$this->assertEquals( 100, has_action( 'after_setup_theme', array( $wp_customize, 'import_theme_starter_content' ) ) );
 	}
 
 	/**
+	 * Test that clearing a fresh site is a no-op if the site is already fresh.
+	 *
+	 * @ticket 38991
+	 */
+	function test_fresh_site_flag_clearing() {
+		global $wp_customize, $wpdb;
+
+		// Make sure fresh site flag is cleared when publishing a changeset.
+		update_option( 'fresh_site', '1' );
+		do_action( 'customize_save_after', $wp_customize );
+		$this->assertEquals( '0', get_option( 'fresh_site' ) );
+
+		// Simulate a new, uncached request.
+		wp_cache_delete( 'alloptions', 'options' );
+		wp_load_alloptions();
+
+		// Make sure no DB write is done when publishing and a site is already non-fresh.
+		$query_count = $wpdb->num_queries;
+		do_action( 'customize_save_after', $wp_customize );
+		$this->assertSame( $query_count, $wpdb->num_queries );
+	}
+
+	/**
 	 * Test WP_Customize_Manager::setup_theme() for frontend.
 	 *
 	 * @covers WP_Customize_Manager::setup_theme()
