After using the addkey function within a script, trying to delete the subkey results in the master key deletion.
The two key are generated outside GPG inside a Tokn , extracted and imported inside GPG.
The extract fom the script that uses the addkey function is the following :
expect << EOF
spawn bash -c "{ echo addkey; echo 13; echo Q; echo 0; echo save; } | gpg -v --debug-level 2 --no-use-agent --command-fd=0 --status-fd=1 --pinentry-mode=loopback --expert --passphrase-file <(echo $PASSWORD) --edit-key $KEY_MAST"
expect "Enter the keygrip: " { send "$KEYRING_SUBKEY\r" }
expect eof
EOF
It gives the following results :
sec rsa1024 2020-09-18 [SC]
64A78F4590247768E1ABE71FCF763B9F719176D0 Keygrip = 36A8042BFF5FB47B9F36AF28D04EB801A3440B8F
uid [ultimate] master (comment) <email>
ssb rsa1024 2020-09-18 [SE]
Keygrip = D877DCDFCB62146D910B85E8830ACE62A3A82EC2
sec rsa1024 2020-09-18 [SC]
A172D251E05E44D8A862C0509EFED945BD1FD0E1 Keygrip = D877DCDFCB62146D910B85E8830ACE62A3A82EC2
uid [ultimate] subkey (comment) <email>
The command : "gpg --delete-secret-and-public-keys subkey", results in a message asking confirmation for the deletion of the master key.
This might be a bug in GPG, as sometimes after several execution of the script in a row, the subkey seems to be added correctly and the deletion message correspond to the one of the subkey.