Azure Key Vault: List Deleted Objects and Recover One
See more Azure Key Vault Examples
Requires Chilkat 11.6.0 or later. Keep qa_data/azure/keyvault.json and token/PFX files private, outside web content and source control, with access restricted to the application identity. Use placeholders replaced by your configuration, never commit credentials. Create output directories before running file-writing examples. These examples target Azure public-cloud vaults, not Managed HSM or sovereign-cloud endpoints.
Uses HTTPS and Key Vault data-plane API 2026-04-01. Configure vault_name and first run the client-credentials token example. It writes access_token and an expires_at UTC timestamp to qa_data/tokens/azureKeyVaultToken.json. Each request checks expiration; refresh is explicit. The sample stops on errors and does not retry. Production callers should handle 429/Retry-After and transient failures with bounded backoff, and reconcile uncertain writes. Redirects are disabled.
Choose object_kind and object_name. Paginates deleted objects, then inspects that object. Set allow_recover=true to recover it; otherwise the example only reads. Requires the corresponding list/get permissions and recover when enabled. A not-yet-visible deleted object can return 404; retry the read later without repeating delete. Purged objects cannot be recovered, and restored availability can be delayed.
Chilkat Delphi DLL Downloads
var
success: Boolean;
stringCompare: HCkStringBuilder;
config: HCkJsonObject;
stringMatch1: Boolean;
validate1: HCkStringBuilder;
alphabet1: HCkStringBuilder;
charIndex1: Integer;
vaultName: HCkStringBuilder;
letters: HCkStringBuilder;
origin: HCkStringBuilder;
http: HCkHttp;
request: HCkHttpRequest;
response: HCkHttpResponse;
result: HCkJsonObject;
token: HCkJsonObject;
stringMatch2: Boolean;
stringMatch3: Boolean;
expires: HCkDateTime;
now: HCkDateTime;
stringMatch4: Boolean;
stringMatch5: Boolean;
stringMatch6: Boolean;
stringMatch7: Boolean;
collection: HCkStringBuilder;
pagePrefix: HCkStringBuilder;
firstPage: HCkStringBuilder;
itemsSeen: HCkHashtable;
itemsItems: HCkJsonArray;
itemsView: HCkJsonArray;
itemsEntry: HCkJsonObject;
itemsCurrentUrl: PWideChar;
itemsPages: Integer;
itemsIndex: Integer;
itemsMore: Boolean;
stringMatch8: Boolean;
itemsNext: HCkStringBuilder;
stringMatch9: Boolean;
stringMatch10: Boolean;
validate52: HCkStringBuilder;
alphabet52: HCkStringBuilder;
charIndex52: Integer;
deletedBase: HCkStringBuilder;
getDeletedUrl: HCkStringBuilder;
stringMatch11: Boolean;
stringMatch12: Boolean;
recoverUrl: HCkStringBuilder;
begin
success := False;
stringCompare := CkStringBuilder_Create();
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Keep this configuration private, outside public web content and source control.
config := CkJsonObject_Create();
CkJsonObject_putStrict(config,True);
success := CkJsonObject_LoadFile(config,'qa_data/azure/keyvault.json');
if (success <> True) then
begin
Memo1.Lines.Add('Could not load private Key Vault configuration.');
Exit;
end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(config,'vault_name'));
stringMatch1 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(config,'vault_name') <> 1) or (stringMatch1 = True)) then
begin
Memo1.Lines.Add('Missing or invalid vault_name.');
Exit;
end;
validate1 := CkStringBuilder_Create();
alphabet1 := CkStringBuilder_Create();
success := CkStringBuilder_Append(validate1,CkJsonObject__stringOf(config,'vault_name'));
if (success <> True) then
begin
Memo1.Lines.Add('Could not validate vault_name.');
Exit;
end;
success := CkStringBuilder_Append(alphabet1,'abcdefghijklmnopqrstuvwxyz0123456789-');
if (success <> True) then
begin
Memo1.Lines.Add('Could not initialize validation.');
Exit;
end;
if ((CkStringBuilder_getLength(validate1) < 3) or (CkStringBuilder_getLength(validate1) > 24)) then
begin
Memo1.Lines.Add('Invalid vault_name length.');
Exit;
end;
charIndex1 := 0;
while (charIndex1 < CkStringBuilder_getLength(validate1)) do
begin
if (CkStringBuilder_Contains(alphabet1,CkStringBuilder__getRange(validate1,charIndex1,1,False),True) <> True) then
begin
Memo1.Lines.Add('Invalid character in vault_name.');
Exit;
end;
charIndex1 := charIndex1 + 1;
end;
vaultName := CkStringBuilder_Create();
CkStringBuilder_Append(vaultName,CkJsonObject__stringOf(config,'vault_name'));
letters := CkStringBuilder_Create();
CkStringBuilder_Append(letters,'abcdefghijklmnopqrstuvwxyz');
if ((CkStringBuilder_Contains(letters,CkStringBuilder__getRange(vaultName,0,1,False),True) <> True) or (CkStringBuilder_EndsWith(vaultName,'-',True) = True) or (CkStringBuilder_Contains(vaultName,'--',True) = True)) then
begin
Memo1.Lines.Add('Invalid vault_name.');
Exit;
end;
origin := CkStringBuilder_Create();
success := CkStringBuilder_Append(origin,'https://');
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct URL or identifier.');
Exit;
end;
success := CkStringBuilder_Append(origin,CkJsonObject__stringOf(config,'vault_name'));
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct URL or identifier.');
Exit;
end;
success := CkStringBuilder_Append(origin,'.vault.azure.net');
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct URL or identifier.');
Exit;
end;
http := CkHttp_Create();
CkHttp_putRequireSslCertVerify(http,True);
CkHttp_putFollowRedirects(http,False);
CkHttp_putConnectTimeout(http,20);
CkHttp_putReadTimeout(http,60);
CkHttp_putVerboseLogging(http,False);
request := CkHttpRequest_Create();
CkHttpRequest_putHttpVerb(request,'GET');
response := CkHttpResponse_Create();
result := CkJsonObject_Create();
CkJsonObject_putStrict(result,True);
// First run Get a Key Vault access token.
token := CkJsonObject_Create();
CkJsonObject_putStrict(token,True);
success := CkJsonObject_LoadFile(token,'qa_data/tokens/azureKeyVaultToken.json');
if (success <> True) then
begin
Memo1.Lines.Add('Could not read the private token file.');
Exit;
end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(token,'access_token'));
stringMatch2 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(token,'access_token') <> 1) or (stringMatch2 = True)) then
begin
Memo1.Lines.Add('Missing or invalid access_token.');
Exit;
end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(token,'expires_at'));
stringMatch3 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(token,'expires_at') <> 1) or (stringMatch3 = True)) then
begin
Memo1.Lines.Add('Missing or invalid expires_at.');
Exit;
end;
expires := CkDateTime_Create();
now := CkDateTime_Create();
success := CkDateTime_SetFromTimestamp(expires,CkJsonObject__stringOf(token,'expires_at'));
if (success <> True) then
begin
Memo1.Lines.Add('Invalid token expiration.');
Exit;
end;
CkHttp_putAuthToken(http,CkJsonObject__stringOf(token,'access_token'));
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(config,'object_kind'));
stringMatch4 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(config,'object_kind') <> 1) or (stringMatch4 = True)) then
begin
Memo1.Lines.Add('Missing or invalid object_kind.');
Exit;
end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(config,'object_kind'));
stringMatch5 := CkStringBuilder_ContentsEqual(stringCompare,'keys',True);
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(config,'object_kind'));
stringMatch6 := CkStringBuilder_ContentsEqual(stringCompare,'secrets',True);
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(config,'object_kind'));
stringMatch7 := CkStringBuilder_ContentsEqual(stringCompare,'certificates',True);
if ((stringMatch5 <> True) and (stringMatch6 <> True) and (stringMatch7 <> True)) then
begin
Memo1.Lines.Add('object_kind must be keys, secrets, or certificates.');
Exit;
end;
collection := CkStringBuilder_Create();
success := CkStringBuilder_Append(collection,CkStringBuilder__getAsString(origin));
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct URL or identifier.');
Exit;
end;
success := CkStringBuilder_Append(collection,'/deleted');
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct URL or identifier.');
Exit;
end;
success := CkStringBuilder_Append(collection,CkJsonObject__stringOf(config,'object_kind'));
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct URL or identifier.');
Exit;
end;
pagePrefix := CkStringBuilder_Create();
success := CkStringBuilder_Append(pagePrefix,CkStringBuilder__getAsString(collection));
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct URL or identifier.');
Exit;
end;
success := CkStringBuilder_Append(pagePrefix,'?');
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct URL or identifier.');
Exit;
end;
firstPage := CkStringBuilder_Create();
success := CkStringBuilder_Append(firstPage,CkStringBuilder__getAsString(pagePrefix));
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct URL or identifier.');
Exit;
end;
success := CkStringBuilder_Append(firstPage,'api-version=2026-04-01&maxresults=25');
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct URL or identifier.');
Exit;
end;
itemsSeen := CkHashtable_Create();
itemsItems := CkJsonArray_Create();
itemsView := CkJsonArray_Create();
itemsEntry := CkJsonObject_Create();
itemsCurrentUrl := CkStringBuilder__getAsString(firstPage);
itemsPages := 0;
itemsMore := True;
while (itemsMore = True) do
begin
if (itemsPages = 10000) then
begin
Memo1.Lines.Add('Page bound reached; enumeration is incomplete.');
Exit;
end;
if (CkHashtable_Contains(itemsSeen,itemsCurrentUrl) = True) then
begin
Memo1.Lines.Add('Repeated nextLink; enumeration is incomplete.');
Exit;
end;
success := CkHashtable_AddStr(itemsSeen,itemsCurrentUrl,'seen');
if (success <> True) then
begin
Memo1.Lines.Add('Could not track pagination.');
Exit;
end;
itemsPages := itemsPages + 1;
success := CkDateTime_SetFromCurrentSystemTime(now);
if (success <> True) then
begin
Memo1.Lines.Add('Could not read current time.');
Exit;
end;
if (CkDateTime_DiffSeconds(expires,now) <= 60) then
begin
Memo1.Lines.Add('Token is expired or near expiration. Obtain a new token before continuing.');
Exit;
end;
success := CkHttp_HttpReq(http,itemsCurrentUrl,request,response);
if (success <> True) then
begin
Memo1.Lines.Add('Request did not complete; inspect diagnostics privately.');
Exit;
end;
if (CkHttpResponse_getStatusCode(response) <> 200) then
begin
Memo1.Lines.Add('HTTP status: ' + IntToStr(CkHttpResponse_getStatusCode(response)));
Memo1.Lines.Add('Request ID: ' + CkHttpResponse__getHeaderField(response,'x-ms-request-id'));
// Do not print full response bodies, tokens, or diagnostic logs.
Memo1.Lines.Add('Request failed. Inspect private diagnostics; reconcile writes before retrying.');
Exit;
end;
success := CkJsonObject_Load(result,CkHttpResponse__bodyStr(response));
if (success <> True) then
begin
Memo1.Lines.Add('Expected a well-formed JSON response.');
Exit;
end;
if (CkJsonObject_JsonTypeOf(result,'value') <> 4) then
begin
Memo1.Lines.Add('Expected a value array.');
Exit;
end;
success := CkJsonObject_ArrayOf2(result,'value',itemsView);
if (success <> True) then
begin
Memo1.Lines.Add('Could not read certificate array.');
Exit;
end;
success := CkJsonArray_Load(itemsItems,CkJsonArray__emit(itemsView));
if (success <> True) then
begin
Memo1.Lines.Add('Could not detach the page before nested requests.');
Exit;
end;
itemsMore := False;
if (CkJsonObject_HasMember(result,'nextLink') = True) then
begin
if (CkJsonObject_JsonTypeOf(result,'nextLink') <> 6) then
begin
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(result,'nextLink'));
stringMatch8 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(result,'nextLink') <> 1) or (stringMatch8 = True)) then
begin
Memo1.Lines.Add('Missing or invalid nextLink.');
Exit;
end;
itemsCurrentUrl := CkJsonObject__stringOf(result,'nextLink');
itemsNext := CkStringBuilder_Create();
success := CkStringBuilder_Append(itemsNext,itemsCurrentUrl);
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct URL or identifier.');
Exit;
end;
if ((CkStringBuilder_StartsWith(itemsNext,CkStringBuilder__getAsString(pagePrefix),True) <> True) or (CkStringBuilder_Contains(itemsNext,'#',True) = True) or (CkStringBuilder_Contains(itemsNext,'\',True) = True) or (CkStringBuilder_Contains(itemsNext,#13,True) = True) or (CkStringBuilder_Contains(itemsNext,#10,True) = True)) then
begin
Memo1.Lines.Add('Unsafe pagination URL; stopped before sending a token.');
Exit;
end;
itemsMore := True;
end;
end;
itemsIndex := 0;
while (itemsIndex < CkJsonArray_getSize(itemsItems)) do
begin
success := CkJsonArray_ObjectAt2(itemsItems,itemsIndex,itemsEntry);
if (success <> True) then
begin
Memo1.Lines.Add('Invalid item.');
Exit;
end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(itemsEntry,'recoveryId'));
stringMatch9 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(itemsEntry,'recoveryId') <> 1) or (stringMatch9 = True)) then
begin
Memo1.Lines.Add('Missing or invalid recoveryId.');
Exit;
end;
Memo1.Lines.Add('Resource ID: ' + CkJsonObject__stringOf(itemsEntry,'recoveryId'));
itemsIndex := itemsIndex + 1;
end;
end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(config,'object_name'));
stringMatch10 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(config,'object_name') <> 1) or (stringMatch10 = True)) then
begin
Memo1.Lines.Add('Missing or invalid object_name.');
Exit;
end;
validate52 := CkStringBuilder_Create();
alphabet52 := CkStringBuilder_Create();
success := CkStringBuilder_Append(validate52,CkJsonObject__stringOf(config,'object_name'));
if (success <> True) then
begin
Memo1.Lines.Add('Could not validate object_name.');
Exit;
end;
success := CkStringBuilder_Append(alphabet52,'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-');
if (success <> True) then
begin
Memo1.Lines.Add('Could not initialize validation.');
Exit;
end;
if ((CkStringBuilder_getLength(validate52) < 1) or (CkStringBuilder_getLength(validate52) > 127)) then
begin
Memo1.Lines.Add('Invalid object_name length.');
Exit;
end;
charIndex52 := 0;
while (charIndex52 < CkStringBuilder_getLength(validate52)) do
begin
if (CkStringBuilder_Contains(alphabet52,CkStringBuilder__getRange(validate52,charIndex52,1,False),True) <> True) then
begin
Memo1.Lines.Add('Invalid character in object_name.');
Exit;
end;
charIndex52 := charIndex52 + 1;
end;
deletedBase := CkStringBuilder_Create();
success := CkStringBuilder_Append(deletedBase,CkStringBuilder__getAsString(origin));
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct URL or identifier.');
Exit;
end;
success := CkStringBuilder_Append(deletedBase,'/deleted');
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct URL or identifier.');
Exit;
end;
success := CkStringBuilder_Append(deletedBase,CkJsonObject__stringOf(config,'object_kind'));
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct URL or identifier.');
Exit;
end;
success := CkStringBuilder_Append(deletedBase,'/');
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct URL or identifier.');
Exit;
end;
success := CkStringBuilder_Append(deletedBase,CkJsonObject__stringOf(config,'object_name'));
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct URL or identifier.');
Exit;
end;
getDeletedUrl := CkStringBuilder_Create();
success := CkStringBuilder_Append(getDeletedUrl,CkStringBuilder__getAsString(deletedBase));
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct URL or identifier.');
Exit;
end;
success := CkStringBuilder_Append(getDeletedUrl,'?api-version=2026-04-01');
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct URL or identifier.');
Exit;
end;
success := CkDateTime_SetFromCurrentSystemTime(now);
if (success <> True) then
begin
Memo1.Lines.Add('Could not read current time.');
Exit;
end;
if (CkDateTime_DiffSeconds(expires,now) <= 60) then
begin
Memo1.Lines.Add('Token is expired or near expiration. Obtain a new token before continuing.');
Exit;
end;
CkHttpRequest_putHttpVerb(request,'GET');
success := CkHttp_HttpReq(http,CkStringBuilder__getAsString(getDeletedUrl),request,response);
if (success <> True) then
begin
Memo1.Lines.Add('Request did not complete; inspect diagnostics privately.');
Exit;
end;
if (CkHttpResponse_getStatusCode(response) <> 200) then
begin
Memo1.Lines.Add('Unexpected HTTP status; inspect request ID and diagnostics privately. Reconcile writes before retrying.');
Exit;
end;
success := CkJsonObject_Load(result,CkHttpResponse__bodyStr(response));
if (success <> True) then
begin
Memo1.Lines.Add('Invalid JSON response.');
Exit;
end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(result,'recoveryId'));
stringMatch11 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(result,'recoveryId') <> 1) or (stringMatch11 = True)) then
begin
Memo1.Lines.Add('Missing or invalid recoveryId.');
Exit;
end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(result,'recoveryId'));
stringMatch12 := CkStringBuilder_ContentsEqual(stringCompare,CkStringBuilder__getAsString(deletedBase),True);
if (stringMatch12 <> True) then
begin
Memo1.Lines.Add('Unexpected recovery identifier.');
Exit;
end;
Memo1.Lines.Add('Recovery ID: ' + CkJsonObject__stringOf(result,'recoveryId'));
if (CkJsonObject_JsonTypeOf(config,'allow_recover') = 5) then
begin
if (CkJsonObject_BoolOf(config,'allow_recover') = True) then
begin
recoverUrl := CkStringBuilder_Create();
success := CkStringBuilder_Append(recoverUrl,CkStringBuilder__getAsString(deletedBase));
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct URL or identifier.');
Exit;
end;
success := CkStringBuilder_Append(recoverUrl,'/recover?api-version=2026-04-01');
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct URL or identifier.');
Exit;
end;
success := CkDateTime_SetFromCurrentSystemTime(now);
if (success <> True) then
begin
Memo1.Lines.Add('Could not read current time.');
Exit;
end;
if (CkDateTime_DiffSeconds(expires,now) <= 60) then
begin
Memo1.Lines.Add('Token is expired or near expiration. Obtain a new token before continuing.');
Exit;
end;
CkHttpRequest_putHttpVerb(request,'POST');
success := CkHttp_HttpReq(http,CkStringBuilder__getAsString(recoverUrl),request,response);
if (success <> True) then
begin
Memo1.Lines.Add('Request did not complete; inspect diagnostics privately.');
Exit;
end;
if (CkHttpResponse_getStatusCode(response) <> 200) then
begin
Memo1.Lines.Add('Unexpected HTTP status; inspect request ID and diagnostics privately. Reconcile writes before retrying.');
Exit;
end;
success := CkJsonObject_Load(result,CkHttpResponse__bodyStr(response));
if (success <> True) then
begin
Memo1.Lines.Add('Invalid JSON response.');
Exit;
end;
Memo1.Lines.Add('Recovery accepted; verify availability before resuming consumers.');
end;
end;
Memo1.Lines.Add('Example completed.');
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(config);
CkStringBuilder_Dispose(validate1);
CkStringBuilder_Dispose(alphabet1);
CkStringBuilder_Dispose(vaultName);
CkStringBuilder_Dispose(letters);
CkStringBuilder_Dispose(origin);
CkHttp_Dispose(http);
CkHttpRequest_Dispose(request);
CkHttpResponse_Dispose(response);
CkJsonObject_Dispose(result);
CkJsonObject_Dispose(token);
CkDateTime_Dispose(expires);
CkDateTime_Dispose(now);
CkStringBuilder_Dispose(collection);
CkStringBuilder_Dispose(pagePrefix);
CkStringBuilder_Dispose(firstPage);
CkHashtable_Dispose(itemsSeen);
CkJsonArray_Dispose(itemsItems);
CkJsonArray_Dispose(itemsView);
CkJsonObject_Dispose(itemsEntry);
CkStringBuilder_Dispose(itemsNext);
CkStringBuilder_Dispose(validate52);
CkStringBuilder_Dispose(alphabet52);
CkStringBuilder_Dispose(deletedBase);
CkStringBuilder_Dispose(getDeletedUrl);
CkStringBuilder_Dispose(recoverUrl);