As a workaround, I suggest deleting the credentials in the existing *.qgs file.
Here is the python code that does this:
import os
def delete_properties(qgs_file, properties):
"""
Only works with *.qgs (not *.qgz - it is a zipped *.qgs)
:param qgs_file: Path to *.qgs file
:param properties: Properties to be replaced with empty string => ''
:return: Return a new *.qgs file without selected properties
"""
dirname = os.path.dirname(qgs_file)
name, extention = os.path.splitext(os.path.basename(qgs_file))
new_path = os.path.join(dirname, name + '_NO_CREDENTIALS' + extention)
with open(qgs_file, 'r', encoding='utf-8') as input_qgs, \
open(new_path, 'w', encoding='utf-8') as output_qgs:
for line in input_qgs.readlines():
new_line = line
for prop in properties:
if prop in new_line:
new_list = []
first_part, second_part = new_line.split(f"{prop}='")
other_parts = second_part.split(f"'")
new_list.append(first_part + f"{prop}='")
new_list.extend(other_parts[1:])
new_line = "'".join(new_list)
output_qgs.write(new_line)
print(f'\nCredentials deleted, file saved:\n{new_path}')
if __name__ == '__main__':
qgs_file_ = r"C:\QGIS_project.qgs"
properties_ = ['password', 'user']
delete_properties(qgs_file_, properties_)