diff --git a/gdb/cli/cli-script.c b/gdb/cli/cli-script.c
index 79c499a4c2..6900f5c9ee 100644
--- a/gdb/cli/cli-script.c
+++ b/gdb/cli/cli-script.c
@@ -1397,11 +1397,13 @@ do_define_command (const char *comname, int from_tty,
 
       if (c->theclass == class_user || c->theclass == class_alias)
 	{
-	  /* if C is a prefix command, tell the user its subcommands will
-	     be kept, and ask if ok to redefine the command.  */
+	  /* if C is a prefix command that was previously defined,
+	     tell the user its subcommands will be kept, and ask
+	     if ok to redefine the command.  */
 	  if (c->prefixlist != nullptr)
-	    q = query (_("Keeping subcommands of prefix command \"%s\".\n"
-			 "Redefine command \"%s\"? "), c->name, c->name);
+	    q = c->user_commands.get () == nullptr
+	      || query (_("Keeping subcommands of prefix command \"%s\".\n"
+			  "Redefine command \"%s\"? "), c->name, c->name);
 	  else
 	    q = query (_("Redefine command \"%s\"? "), c->name);
 	}
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 17f357832c..2c30ea657e 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -26564,6 +26564,11 @@ Example:
 @example
 (gdb) define-prefix abc
 (gdb) define-prefix abc def
+(gdb) define abc def
+Type commands for definition of "abc def".
+End with a line saying just "end".
+>echo command initial def\n
+>end
 (gdb) define abc def ghi
 Type commands for definition of "abc def ghi".
 End with a line saying just "end".
diff --git a/gdb/testsuite/gdb.base/define-prefix.exp b/gdb/testsuite/gdb.base/define-prefix.exp
index e8f4c3c4e4..b04c5959c0 100644
--- a/gdb/testsuite/gdb.base/define-prefix.exp
+++ b/gdb/testsuite/gdb.base/define-prefix.exp
@@ -98,27 +98,45 @@ gdb_test "abc-prefix def-prefix ghi-prefix-cmd alternate-jkl-cmd" \
 
 
 
-# Now redefine ghi-prefix-cmd as a real command, and check it is working.
+# Now define ghi-prefix-cmd as a real command, and check it is working.
+send_gdb "define abc-prefix def-prefix ghi-prefix-cmd\n"
+gdb_expect {
+    -re "Type commands for definition of \"abc-prefix def-prefix ghi-prefix-cmd\".\r\nEnd with a line saying just \"end\".\r\n>$" {
+	send_gdb "echo defined command ghi-prefix-cmd\\n\nend\n"
+	gdb_expect {
+	    -re "$gdb_prompt $" {pass "define user command: ghi-prefix-cmd"}
+	    timeout {fail "(timeout) define user command: ghi-prefix-cmd"}
+	}
+    }
+    timeout {fail "(timeout) define user command: ghi-prefix-cmd"}
+}
+
+# Verify ghi-prefix-cmd command works.
+gdb_test "abc-prefix def-prefix ghi-prefix-cmd" \
+    "defined command ghi-prefix-cmd" \
+    "use defined user command: ghi-prefix-cmd"
+
+# Now redefine ghi-prefix-cmd, and check it is working.
 send_gdb "define abc-prefix def-prefix ghi-prefix-cmd\n"
 gdb_expect {
     -re "Keeping subcommands of prefix command \"ghi-prefix-cmd\"\.\r\nRedefine command \"ghi-prefix-cmd\".*y or n. $" {
 	send_gdb "y\n"
-	    gdb_expect {
-		-re "Type commands for definition of \"abc-prefix def-prefix ghi-prefix-cmd\".\r\nEnd with a line saying just \"end\".\r\n>$" {
-		    send_gdb "echo redefined command ghi-prefix-cmd\\n\nend\n"
-		    gdb_expect {
-			-re "$gdb_prompt $" {pass "redefine user command: ghi-prefix-cmd"}
-			timeout {fail "(timeout) redefine user command: ghi-prefix-cmd"}
-		    }
+	gdb_expect {
+	    -re "Type commands for definition of \"abc-prefix def-prefix ghi-prefix-cmd\".\r\nEnd with a line saying just \"end\".\r\n>$" {
+		send_gdb "echo redefined command ghi-prefix-cmd\\n\nend\n"
+		gdb_expect {
+		    -re "$gdb_prompt $" {pass "redefine user command: ghi-prefix-cmd"}
+		    timeout {fail "(timeout) redefine user command: ghi-prefix-cmd"}
 		}
-		timeout {fail "(timeout) redefine user command: ghi-prefix-cmd"}
 	    }
+	    timeout {fail "(timeout) redefine user command: ghi-prefix-cmd"}
+	}
     }
-    -re "$gdb_prompt $"	{fail "redefine user command: ghi-prefix-cmd"}
+    -re "$gdb_prompt $" {fail "redefine user command: ghi-prefix-cmd"}
     timeout {fail "(timeout) redefine user command: ghi-prefix-cmd"}
 }
 
-# Verify ghi-prefix-cmd command works.
+# Verify redefined ghi-prefix-cmd command works.
 gdb_test "abc-prefix def-prefix ghi-prefix-cmd" \
     "redefined command ghi-prefix-cmd" \
     "use redefined user command: ghi-prefix-cmd"
