You can programmatically rename an already existing file in the media library using rename file API. This operation would rename all file versions of the file. Note: The old URLs will stop working. The file/file version URLs cached on CDN will continue to work unless a purge is requested.
Headers
Name
Type
Description
Authorization
string
base64 encoding of your_private_api_key:
Note the colon in the end.
Request Body
Name
Type
Description
filePath
string
The full path of the file you want to rename. For example - /path/to/file.jpg
newFileName
string
The new name of the file. A filename can contain:
- Alphanumeric Characters: a-z, A-Z, 0-9 (including Unicode letters, marks, and numerals in other languages).
- Special Characters: ., _, and -.
Any other character, including space, will be replaced by _.
purgeCache
boolean
Option to purge cache for the old file URL. When set to true, it will internally issue a purge cache request on CDN to remove cached content on the old URL. E.g. if an old file was accessible at - https://ik.imagekit.io/demo/old-filename.jpg, a purge cache request will be issued to remove the CDN cache for this URL. This purge request is counted against your monthly purge quota. Note: Cache will be purged only for the current version of file.
Default value - false
// When purgeCache is set to true{ purgeRequestId:"598821f949c0a938d57563bd"}// When purgeCache is not set or set to false, response is empty{}
{ "message" : "File renamed successfully but we could not purge the CDN cache for old URL because of rate limits on purge API.",
"help" : "For support kindly contact us at support@imagekit.io .","reason" : "PURGE_FAILED"}
{"message" : "No file found in media library at filePath /path/to/file.jpg","help" : "For support kindly contact us at support@imagekit.io .","reason" : "FILE_MISSING"}
{"message" : "File with name newFileName already exists at the same location.","help" : "For support kindly contact us at support@imagekit.io .","reason" : "FILE_ALREADY_EXISTS"}
Response structure and status code
In case of an error, you will get an error code along with the error message. On success, you will receive a 200 status code with the JSON-encoded response body.
The response depends upon the value of purgeCache in the request. If purgeCache is set to true, the response contains purgeRequestId, which can be used to get the purge cache status.
// When purgeCache is set to true, we send the purge request ID.// You can use this to get purge status{ purgeRequestId:"598821f949c0a938d57563bd"}
// When purgeCache is set to false or not set{}
Example
Here is an example request to understand API usage.