Delete Files in Drupal using the File Delete Module

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi i'm ivan from webwash and in this video i'll show you how to use the file delete module something you may not be aware of is that deleting files in drupal isn't as simple as deleting a piece of content there's a lot of mechanics behind deleting a file because files are often attached to an article or a basic page so they often reference other pieces of content so you really can't just go in there and delete a file now drupal has this concept of like um permanent and temporary status of a file so when a file is is permanent and i'll show you that in this video files won't be deleted but when the status changes to temporary then after six hours which is the default but you can change this settings after six hours after chronos run from six hours then it'll automatically go in there and delete files but if you want to go straight away and delete files right now well it doesn't quite work as simple as deleting an article or basic page the first module i want to show you is firewall delete so if you want to learn more about the module just head over to drupal.org project slash file underscore delete and what i'll do is i will jump over to my terminal as i often have it let me just make sure this is the correct thing yep it is and then i will download it using composer so just composer require drupal slash file underscore delete now give that a second or two i am running uh composer two so it is pretty quick and it's also been cached because i tested this out earlier okay so we have gone ahead and downloaded the module and now let me jump over to my site and then we'll go to extend and then we'll jump and search for file here we go file delete and we'll simply install it so just give that a second or two to install and then once that's installed i will prepare my files which i'll be uploading let me bring that up so here i've got a few test files that i can go ahead and install so i'll just give that a second to install and and then if we go to content and then and then files from this page when you upload a file you should see it appear on this page okay so what i'll do is i'll go to content add content and let me select article i will enter in test article put in a bunch of basic text and then i will drag over one of my great library of assets what is this article.jpg yep that will do we'll just upload this and i will give it the alternative text of article text and then we click on save so far so good and then here you can see an article with and with an image attached okay now if we go to content go to files we should see our image right here now this this files page will list out any files or images which have been attached to an image field or a file field so how do we go ahead and delete this file okay and and here you can see the uh the status which i was mentioning called permanent if we have looking here we can see where it's actually used because drupal drupal treats this as a managed files now to use file manage we have to modify this page and luckily this page is just a view so if we hover up the top here and then click on view article sorry no what is it edit view sorry we click on add in the fields we need to add a custom field and this custom field is called link to delete file we add that link in and you can go ahead and change the label you can even change the text that you want displayed save and then we can scroll down and we can see this file right here called delete let's go ahead and save that and then once this is saved we can see our link to delete file right here now if we were to click on this it will take us to a confirmation page perfect and then if we click on delete file it's going to spit up an error and it's going to say the file article.jpg jpeg can't be deleted because it is used by the following modules file and essentially what i'm going to do now is i'm going to show you the actual database what's happening let me jump over so this is the so the tables on the right i hope you can see this in the video but i can't zoom this in which is unfortunate but these are these are just drupal tables and if we look at the file managed we can see the actual file and the reason why it's called file managed is that a copy of the file is on the file system but it's also managed by drupal so drupal's aware of it but most importantly it actually tracks the usage so if we search for if we filter just by file we can see that this file id1 is being managed so that means it's actually attached to to some article via a field so that's why we cannot delete it so let's go ahead and edit this and click on remove and save it and we've gone ahead and deleted the file so we should be good right we go here we have a look here no it's not there awesome simple we should be able to go to content files and then click on delete click on delete file and again it's saying it cannot be deleted and most importantly if you click on this used in this pretty much tells you what's in what's in the file usage table and you can see that it's still attached to the test article article why is that well the reason for that is the article has uh revisions turned on so the image has been removed from the current published revision but it's still referenced in the other revisions so we need to go ahead and edit the test article go to revisions if we click on this first revision you can see that it's there and so if we go ahead and delete it now we have deleted the revision and what you need to do and i figured this out while i was testing this stuff out let me just clear this up you need to then run well let me just do that in a second let's go back to content files and here you can still see used in one place why is that well you need to rebuild the site cache give that a second or two and then if we refresh this used in should be zero and then we click on delete click on delete file it has been deleted but it's still there and if we look in the file system so let's jump to another one and it is right here we can still see we can still see our article is here in the files directory now the status has been changed to temporary so drupal will delete this in the next six hours when cron is run and if we go to configuration and i believe yep configuration and it is file system you can change it here so the shortest time span is six hours all the way up to three months or you can even tell drupal to not delete any temporary files which i would recommend because you may run out of disk space but you can you can actually tell it to stop deleting all files now this module yes it works well but one limitation it doesn't delete the file straight away so if you want to delete it straight away what i'm going to do now is show you another workaround using drush which will delete the file quickly and instantly so let's jump over to our terminal and let me jump over to the next tab and if we just run drush okay we're going to get a whole bunch of options here let's type in grep what is an entity i believe i know can't spell delete entity okay so it is this delete entity okay so if i type in drash help just to make sure it's all working so what we can do is we can use drush entity delete then the actual the entity type which will be file and then the id now be careful with this because if you if you just type in entity delete shortcut which is the entity name you can easily just delete all your content so be very careful with this so what i want to do is now show you how to get the actual id so there are a few ways if we jump back here and then we click on say this zero places you'll see in the actual tool in the address bar sorry you have admin content files usage and then the id which is one this being the first file and then if we jump into the database and refresh well no let's go to file manage here we can also see fid file id is right there so all we need to do is type in drush well let me bring that up to the to the top one sec so you can see it okay drush file and then id then it is going to go ahead and delete it and then if we go back and refresh okay the file looks gone well technically the row from the table is deleted and if we look in the database table we can see that the row has been deleted perfect now let's check out the file on the file system to make sure that it's actually deleted here is article jpeg and if we list the directory you can see that it's no longer there so it has been deleted so that is a quick overview of file delete and also how did how to delete files using drush if possible if you need to delete a whole bunch of files just go ahead and and you also want to delete the files straight away then look at using drush if you want to give your editors the ability to delete files which will then be set to a temporary status and then deleted in the future depending on how your site has been configured then look at using file or delete so that is it from me thank you so much for watching if you want to learn more about drupal just head over to webwatch.net and also don't forget to subscribe to this youtube channel i have to say it and that's it from me thank you so much and i'll catch you next time
Info
Channel: WebWash
Views: 613
Rating: 5 out of 5
Keywords:
Id: 7bY6HFUFyfk
Channel Id: undefined
Length: 12min 37sec (757 seconds)
Published: Sun Apr 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.