Index: src/wp-includes/ms-site.php
===================================================================
--- src/wp-includes/ms-site.php	(revision 47912)
+++ src/wp-includes/ms-site.php	(working copy)
@@ -69,9 +69,10 @@
 		return new WP_Error( 'db_insert_error', __( 'Could not insert site into the database.' ), $wpdb->last_error );
 	}
 
-	clean_blog_cache( $wpdb->insert_id );
+	$insert_id = $wpdb->insert_id;
+	clean_blog_cache( $insert_id );
 
-	$new_site = get_site( $wpdb->insert_id );
+	$new_site = get_site( $insert_id );
 
 	if ( ! $new_site ) {
 		return new WP_Error( 'get_site_error', __( 'Could not retrieve site data.' ) );
Index: tests/phpunit/tests/multisite/site.php
===================================================================
--- tests/phpunit/tests/multisite/site.php	(revision 47912)
+++ tests/phpunit/tests/multisite/site.php	(working copy)
@@ -1387,6 +1387,26 @@
 		}
 
 		/**
+		 * @ticket 50324
+		 */
+		public function test_wp_insert_site_with_clean_site_cache() {
+			remove_action( 'wp_initialize_site', 'wp_initialize_site', 10 );
+			add_action( 'clean_site_cache', array( $this, 'action_database_insert_on_clean_site_cache' ) );
+
+			$site_id = wp_insert_site( array(
+				'domain'     => 'valid-domain.com',
+				'path'       => '/valid-path/',
+				'network_id' => 1,
+			) );
+
+			$this->assertInternalType( 'integer', $site_id );
+
+		}
+
+		public function action_database_insert_on_clean_site_cache() {
+			update_site_option( 'database_write_test.'.time(), true );
+		}
+		/**
 		 * @ticket 40364
 		 */
 		public function test_wp_insert_site_empty_domain() {
