I have implemented a file deletion method in my Ionic 4 App. Using the deleteFile() method, I can successfully delete individual files. However, when attempting to delete multiple files using a forEach loop, the process fails after the first deletion.
HTML
<ion-card *ngFor="let data of list; let i = index">
<ion-card-content>
<div>
<div *ngFor="let n of loop; let n = index">
<div class="linkButtons">
<ion-button fill="outline" color="blackgreen" (click)="DeleteAllFiles()">delete all Photos</ion-button>>
</div>
</div>
</div>
<div>
<ion-list>
<ion-item *ngFor="let doc of documents; index as pos" class="ion-text-wrap">
<ion-icon name="document" slot="start"></ion-icon>
<ion-textarea auto-grow="true"><small>{{ doc.name }}</small></ion-textarea>
<ion-button slot="end" fill="clear" (click)="deleteFile(doc, pos)">
<ion-icon slot="icon-only" name="trash"></ion-icon>
</ion-button>
</ion-item>
</ion-list>
</div>
</ion-card-content>
</ion-card>
The usage of (click)="deleteFile(doc, pos) works correctly.
However, (click)="DeleteAllFiles() fails after the first deletion.
TypeScript
DeleteAllFiles(): void {
this.documents.forEach(
(imgEntry, position ) => this.deleteFile(imgEntry, position));
}
deleteFile(imgEntry, position) {
this.documents.splice(position, 1);
this.storage.get('app_DOCS').then(documents => {
let arr = JSON.parse(documents);
let filtered = arr.filter(name => name != imgEntry.name);
this.storage.set('app_DOCS', JSON.stringify(filtered));
let correctPath = imgEntry.filePath.substr(0, imgEntry.filePath.lastIndexOf('/') + 1);
this.file.removeFile(correctPath, imgEntry.name).then(res => {
this.presentToast('File removed.');
});
});
}
Console Logs
List of Documents
Documents [
{
"name":"UUID_5deb6fae-4d84-41ac-8157-01f9c68e73c5_answerID_155_originalName_icon-144x144.png",
"path":"ionic://localhost/_app_file_/var/mobile/Containers/Data/Application/F8772763-A219-407D-92CD-EF5178F0EA9C/Library/NoCloud/UUID_5deb6fae-4d84-41ac-8157-01f9c68e73c5_answerID_155_originalName_icon-144x144.png",
"filePath":"file:///var/mobile/Containers/Data/Application/F8772763-A219-407D-92CD-EF5178F0EA9C/Library/NoCloud/UUID_5deb6fae-4d84-41ac-8157-01f9c68e73c5_answerID_155_originalName_icon-144x144.png"
},
{
"name":"UUID_5deb6fae-4d84-41ac-8157-01f9c68e73c5_answerID_155_originalName_44198511-How-to-Fix-FAL-Rifle-Brass-Strikes.pdf",
"path":"ionic://localhost/_app_file_/var/mobile/Containers/Data/Application/F8772763-A219-407D-92CD-EF5178F0EA9C/Library/NoCloud/UUID_5deb6fae-4d84-41ac-8157-01f9c68e73c5_answerID_155_originalName_44198511-How-to-Fix-FAL-Rifle-Brass-Stri...
]
Working Code
deleteFile(imgEntry, position): Promise<any> {
console.log("deleteFile selected");
let request = this.storage.get(this.STORAGE_KEY_DOC);
console.log("position", position);
request.then(documents => {
let arr = JSON.parse(documents);
this.documents.splice(position);
let correctPath = imgEntry.filePath.substr(0, imgEntry.filePath.lastIndexOf('/') + 1);
this.file.removeFile(correctPath, imgEntry.name).then(res => {
let resp = JSON.stringify(res);
console.log("resp", resp);
});
let removeDB = this.storage.set(this.STORAGE_KEY_DOC, JSON.stringify(this.documents));
}).then(() => {
});
return request;
}