How to delete files inside "livewire-tmp" folder | Laravel Livewire Nuggets

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone trish all are doing well this is a question that was asked by one of our viewers is there a way to basically clean up the livewire theme folder of images or files or whatever the case may be so in this video we're just going to address the old images you can obviously apply that to any other files that you might have all right so let's quickly go to that folder around the under storage app laugh wiretap you'll see this is the folder we are talking about now the thing is it stores kind of temporary files right here before it moves it into the relevant folder when you store it so we created a livewire post we hate component with if we when we use the file uploads then we stored the post basically post create the image we store the image with the path inside an image folder inside the public directory okay so it's let's quickly go there let me just show you where that is and the storage app inside the public folder we create an image folder that's what it is right there and then we store the title and then we dispatch an event to the browser all right now the thing is let me just start off with the options number one you can create a cron job to actually do this in the background so if you open up your terminal you can in order to create one you would go to php artisan make command and you can call it whatever you want i'm not going to be dealing with that one in this episode so if you want to go and read more just go and read the documentation and you can create that so the two that i'm going to be talking about is the modify or the cli hate one right there but before i do the modify the cleanup uploads file method right there i want to kind of explain how livewire actually handles the file uploads so let's quickly go to our component under create component in order to do the uploads we need to use the use for with file upload so let's quickly go into that trade right there so in the trade you will see start upload then we have the finished upload so basically when it finished uploading the file alright so in this case what it will do so once it's finished it calls a method called clean up old uploads all right so what that does is let's quickly go to that method all right now that method if it's an s3 if it's using an s3 disk it's basically returns it's not going to do anything that's a different method that you have to do in order to deal with s3s so this is just only working for local so that's the one that we're going to be dealing with all right so what it does is it will loop over all the files that's in there okay but then it will check them for a time step so in this case it's yesterday's timestamp then it will just check the now so basically the time that you have now then it will check for a day from now then it will just check that and it will pull out this timestamp so if yesterday's time stamp is basically in this case greater than when the storage file was last modified then it will actually call that file and it will delete it this is an automatic behavior of live wire with file uploads now this method as you can see it any only will get called when we're going to create another post okay so that's what it will do so let's say you don't didn't create any post let's say for a week those files will remain there until you create a post okay so that's one way to do it okay so you don't have to worry about that it will automatically clean it when you create another post and upload an image then it will delete that now let's say you want to modify that behavior let's say you want to change it to be let's say once every hour okay so what we can do is we can copy that method that protected method let's just copy it so this one right here so copy that and basically bring it inside your component and then you can modify it so let's quickly go there all right now this method will override the default behavior right so now this can be yesterday timestamp okay so like this but now what we can do is we can change it and give it a different name but for simplicity's sake i'm not going to do that i can just say now and i can just change this to be an hour like this or you can say hour you'll basically be one hour and if you said hours here you can specify the amount of hours let's say two hours so every two hours post that's been last modified it will delete them so that's the one way and the easiest way to do that but just remember this method only gets called once you create another post so that that files inside your storage app this will not get deleted until you create another post because that method is not going to be called so that's why if you want to do that automatically you will create a cron job for that okay but let's say there's another way to do it now let's say you want to have more freedom in your dashboard you want to create a button where you can actually just click the button to delete it there's a way we can do that we can call this method or we can create our own so let's quickly do that so we can create a public function and we're going to call this one let's say clean up [Music] let's say clean up old temp images just as an example now the thing is let's just get this like old files i'm just going to get all the old files and then we're just going to call on the storage all right and then we're just going to get the files but which files and which directory as you can see the directory right so the directory that we want to look in is in the live wire depth like this all right and what i want to do let me just die and dump the old files so that we can actually see if we can view that files right so let's quickly create a button that will call this method so let's quickly go now create posts right here so i don't want to be in our form i just want to go outside of form and just create a diff again with a space y i'm just using this an example guys so if you guys want to organize it a little bit better you guys can do that all right so just copy whatever i'm doing right so let's quickly say space my f10 okay inside here i'm just going to create a button and we'll call it method so actually button and i'm just going to say clean up okay so now the thing is what we're going to do is we're going to do a wire click all right and what we're going to do we're going to call that method basically if we click the button while clean up that old images all right so let's see if this work right so this basically the button i have so this is for the create post so you can you can kind of create your own component or do whatever you guys want to do but this is i'm just you you're doing the the simplest most fastest way possible all right so let's say if i clean this button i need to get that down down so let me click it as you can see it brings an array of all the livewire them files right there so as you can see let me just zoom in a bit more so as you can see the lifewire temp with all that images in there okay so let me just close this off so now what i want to do i want to loop over those images and individually delete them okay so let's do that right so what i want to do now is i want to create them for each loop so let's do it for each right so for each of the old files as file okay so then we can just do that and then what i want to do now is i just want to say storage storage and then we just want to say delete and then we just want to say that file okay so the files because we remember here we get all the old files and what we want to do that old files we want to loop over them and we just want to say delete so this is just a manual way to do it okay and so if we just press that button okay so you can obviously click this method call it in a command all that kind of stuff right so this we can just send a browser event and just say files deleted it'll kind of keep you things simple so let's see if this works so if i press this button it needs to get all the files it need to loop over them and individually delete them but just make sure you bring in your storage facade right here so let's go there as you can see right there i'm bringing it in so if i click this and then i dispatch this event awesome stuff okay so let's still clean up so let's press this button so as you can see it's busy working right didn't get the message but if we go to the storage app lifeway you can see it deleted all the old files right there awesome stuff okay let's try it again hopefully we get a message as you can see files deleted right there so awesome stuff so that's an end of this episode guys if you have any more questions please ask them for me in the comments section but this is not ideal so i'm just want to mention that so if you guys want to do this manually that's the way to do it but every time when you do an upload that's another way to do it and the best way is obviously to have a cron job running in the background but i know it's not possible for some websites because some are hosted on cpanel where people don't have access to supervisor to actually check if their chrome jobs run in the background all right so anyway guys thank you watch the video and if you like the video please give it a like if you have any other things that you want to add please add them in the comment section and see you guys in the next one adios
Info
Channel: AngelJay Academy
Views: 174
Rating: undefined out of 5
Keywords:
Id: EJMe9_MZO1g
Channel Id: undefined
Length: 9min 50sec (590 seconds)
Published: Mon Nov 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.