There is no "overwrite" option.
There does not seem to be an "overwrite" or "force" command when adding something. So you manually have to delete first.
(You can look into the source and search for "already.exists". There is no overwrite. It just goes straight to thrown an exception. => https://github.com/openjdk/jdk17u/blob/master/src/java.base/share/classes/sun/security/tools/keytool/Main.java)
There is no documented "overwrite" option for either the "-genkey" command or the "-importcert" command. Example below for "-genkey" command.
Generating a new truststore:
$ keytool -keystore keystore.p12 -storepass 123456 -genkey -keyalg RSA -noprompt -dname "CN=test.example.com" -v
Generating 2,048 bit RSA key pair and self-signed certificate (SHA256withRSA) with a validity of 90 days
for: CN=test.example.com
[Storing keystore.p12]
✓
Overwriting does NOT work:
$ keytool -keystore keystore.p12 -storepass 123456 -genkey -keyalg RSA -noprompt -dname "CN=test.example.com" -v
keytool error: java.lang.Exception: Key pair not generated, alias <mykey> already exists
java.lang.Exception: Key pair not generated, alias <mykey> already exists
at java.base/sun.security.tools.keytool.Main.doGenKeyPair(Main.java:1930)
at java.base/sun.security.tools.keytool.Main.doCommands(Main.java:1171)
at java.base/sun.security.tools.keytool.Main.run(Main.java:415)
at java.base/sun.security.tools.keytool.Main.main(Main.java:408)
✗
$ keytool -keystore keystore.p12 -storepass 123456 -list
Keystore type: PKCS12
Keystore provider: SUN
Your keystore contains 1 entry
mykey, Mar 13, 2024, PrivateKeyEntry,
Certificate fingerprint (SHA-256): 99:56:B4:19:E8:02:38:39:C4:01:67:08:EB:37:25:B8:15:CB:23:AE:CE:A1:15:44:0D:B4:B4:17:82:0D:D8:89
✓
So you have to manually delete:
$ keytool -keystore keystore.p12 -storepass 123456 -delete -alias mykey -v
[Storing keystore.p12]
✓
$ keytool -keystore keystore.p12 -storepass 123456 -delete -alias mykey -v
keytool error: java.lang.Exception: Alias <mykey> does not exist
java.lang.Exception: Alias <mykey> does not exist
at java.base/sun.security.tools.keytool.Main.doDeleteEntry(Main.java:1654)
at java.base/sun.security.tools.keytool.Main.doCommands(Main.java:1149)
at java.base/sun.security.tools.keytool.Main.run(Main.java:415)
at java.base/sun.security.tools.keytool.Main.main(Main.java:408)
✗
$ keytool -keystore keystore.p12 -storepass 123456 -list
Keystore type: PKCS12
Keystore provider: SUN
Your keystore contains 0 entries
✓
And then it works:
$ keytool -keystore keystore.p12 -storepass 123456 -genkey -keyalg RSA -noprompt -dname "CN=test.example.com" -v
Generating 2,048 bit RSA key pair and self-signed certificate (SHA256withRSA) with a validity of 90 days
for: CN=test.example.com
[Storing keystore.p12]
✓
$ keytool -keystore keystore.p12 -storepass 123456 -list
Keystore type: PKCS12
Keystore provider: SUN
Your keystore contains 1 entry
mykey, Mar 13, 2024, PrivateKeyEntry,
Certificate fingerprint (SHA-256): 91:DA:5C:EA:AA:65:83:A2:D4:7B:27:5E:44:09:4E:8B:5F:C2:FD:87:94:03:E7:83:18:CD:10:D9:C9:E0:F8:7E
✓