We will show you how to delete files, when updating database records. We have an update record page and we want to delete the old image, from our server when updating a certain product's image. You already know how to create an update record page from our updating database records tutorial, so we are not going to show how to build one in this tutorial Right click the file upload step, in your update record action. Open the database actions menu. And add a database query (1). Click the query options button (2). Select, and add the table, which you are using in your database update step (3). Then add its fields to the columns select list (4). Switch to the filters tab (5).
And add the ID column, to the filters list. then, assign a value to it.
Select the same POST variable which you are using as a filter in the database update step. Click select.
And then, click the OK button. Now, right click the query, that we've just created. And add a repeat (1). Select an expression for the repeat (2). You should select the query, that we created, as an expression (3). Then, click Select.
And right click the steps, inside the repeat. We need to add a condition here, which will check if there is a file uploaded. So the file delete will only be executed, when there is a new file uploaded. Select add condition (1). Then, click the dynamic data button, in order to select a condition (2). Under the file upload step, select path. This will check, if there is a new image uploaded. Click select (3).
And now, we will ad the file delete step. It must be added under then, in the condition step. Right click steps, under then. Open the file management menu. And select add file remove.
Click the dynamic button, in order to select the file path (1). We store the file name in our database. So we select the image binding returned by the repeater (2).
This only returns the file name, so we manually add the folder, where we upload our images to. Now we have the full path to the image, that should be deleted. Save your action file. And we are done - let's preview the results. You see that in our upload folder we have 3 images - for our 3 database records. Let's update one of the records now. We select a new image, and then click the update product button. The new image is uploaded, stored into the database and the old one is removed, from the uploads folder. That's how easy it is to remove old files, when updating database records.