Appearance
Delete Images from the Command Line
The optimole delete command of the optimole command-line tool deletes images from your Optimole library. It deletes every image of a folder and its subfolders, every image of one of your websites, or the images in a list of asset keys. It does the same as Delete all images in the dashboard's Library, without the 100-image limit of a selection.
In this article
❗ Data loss risk: Deleted images cannot be restored from the dashboard or the CLI. Their Optimole URLs stop working, so pages that still use them show broken images. This includes the images a website offloaded to Optimole.
Before You Start
- Install the CLI and connect it to your account, as described in Upload Files to Optimole from the Command Line. If you installed it earlier, run the install command again to update it:
optimole deleteneeds version 0.4.0 or later. - Use your root API key. A sub API key cannot delete images.
Delete Every Image of a Folder
Name the folder by its path in your library:
sh
optimole delete --folder Campaigns/2025The CLI says what it will delete and asks you to type the folder's path to confirm:
text
About to delete every image in the folder "Campaigns/2025" (ID 15) and its subfolders; the folders are kept. This cannot be undone: the CDN stops serving the images.
Type "Campaigns/2025" to confirm: Campaigns/2025
Deleting 1520 images in the background. They leave the library within a few minutes.Any other answer deletes nothing. The folders stay, empty. Images you add to the folder after the command ran are kept.
- Case does not matter in the path. Run
optimole foldersto list your folders with their paths and IDs. - If two folders have the same path, the CLI lists their IDs and deletes nothing. Name the folder by its ID instead:
optimole delete --folder-id 15. - On a website's folder (one of your Allowed Domains), the command also deletes the images the website offloaded to Optimole, and they stop loading on that website.
Delete Every Image of a Website
sh
optimole delete --site example.comThis deletes every image of the website, in any folder. You confirm by typing the domain. A URL works too: https://www.example.com/blog means example.com. Give a domain with non-Latin characters in its xn-- form.
Delete Images by Asset Key
Each image has an asset key: the 32 characters after id: in its URL. optimole upload --json also prints it, as key. Put the keys in a file, one per line, and run:
sh
optimole delete --keys old-keys.txtYou confirm by typing delete. The CLI sends the keys 100 at a time and prints how many images it deleted, for example Deleted 250 images.
- Blank lines are ignored, and a key listed twice counts once.
- If one key of a request is not in your library, or is deleted already, Optimole refuses the whole request and names the key. The run stops there. The CLI says how many keys it deleted before, so you can remove them from the file and run it again.
Use the CLI in Scripts
A script cannot answer the question, so add --yes to delete without asking. Without a terminal, the command refuses to run without --yes:
sh
optimole delete --folder Campaigns/2025 --yesTo read the keys from another command, use --keys -:
sh
printf '%s\n' KEY1 KEY2 | optimole delete --keys - --yesAdd --json to print the result as one JSON object, for example {"scope":"folder","folder_id":15,"folder":"Campaigns/2025","deleting":1520}. Run optimole delete -h to see every option.
Troubleshooting
"Deleting images needs a full-access API key"
The CLI uses a sub API key. Run optimole login with your root API key, or set it in the OPTIMOLE_API_KEY environment variable.
"unknown command" for optimole delete
Your copy of the CLI does not have the delete command yet. Run the install command again to update it, as described in Install the CLI.
"The following keys are invalid or unauthorized"
One of the keys is not an image of your library, or it is deleted already. Remove the keys the message names, and the keys the CLI deleted before, then run the command again.
"The deletion may have started anyway"
When you delete a folder or a website, the connection failed after the CLI sent the request, or Optimole answered with a server error. The CLI cannot tell whether the deletion started, so it does not repeat the request. Wait a few minutes, then open the folder in the dashboard's Library to check whether its images are gone before you run the command again.
