Now and then I try to erase a device (usually a USB flashdrive) with disk utility and it "fails".
The failure alert will usually say that it couldn't unmount the target device.
Then, I REPEAT the procedure, and now the drive unmounts and initializes properly.
OP:
Did you stop after "the first failure"?
If you did, try again.
Also... if a repeat or two doesn't change things, try this:
Erase it, but this time choose a Windows format (such as exFAT).
If the drive now seems to go through with the erasure properly, THEN try erasing it again, this time using a Mac format (either HFS+ or APFS).
If THAT doesn't work, try taking the drive to a Windows computer and see if it will erase there. If it does -- again -- bring it back to the Mac and try a Mac format erasure again.
A word about APFS v. HFS+ ...
Using APFS with a platter-based drive that is used for data (only) storage can result in excessive fragmentation and can cause "disk thrashing".
For that reason, only use APFS where you MUST USE APFS -- such as for a time machine backup. Otherwise, use the old-fashioned HFS+.