diff --git a/main/astobj2.c b/main/astobj2.c
index a9e0d7a36111b53e909a613a7062bd4df30d527c..35dd17172e08b90abd64797388b2b223c82a23ad 100644
--- a/main/astobj2.c
+++ b/main/astobj2.c
@@ -1710,12 +1710,18 @@ static struct ast_cli_entry cli_astobj2[] = {
 	AST_CLI_DEFINE(handle_astobj2_stats, "Print astobj2 statistics"),
 	AST_CLI_DEFINE(handle_astobj2_test, "Test astobj2"),
 };
+
+static void astobj2_cleanup(void)
+{
+	ast_cli_unregister_multiple(cli_astobj2, ARRAY_LEN(cli_astobj2));
+}
 #endif /* AO2_DEBUG */
 
 int astobj2_init(void)
 {
 #ifdef AO2_DEBUG
 	ast_cli_register_multiple(cli_astobj2, ARRAY_LEN(cli_astobj2));
+	ast_register_atexit(astobj2_cleanup);
 #endif
 
 	return 0;