{"id":1493,"date":"2023-11-12T10:24:25","date_gmt":"2023-11-12T10:24:25","guid":{"rendered":"https:\/\/www.devopsfreelancer.com\/blog\/?p=1493"},"modified":"2023-11-30T10:28:59","modified_gmt":"2023-11-30T10:28:59","slug":"what-is-the-difference-between-truncate-and-delete","status":"publish","type":"post","link":"https:\/\/www.devopsfreelancer.com\/blog\/what-is-the-difference-between-truncate-and-delete\/","title":{"rendered":"What is the difference between TRUNCATE and DELETE ?"},"content":{"rendered":"\n<p>TRUNCATE and DELETE are both used to remove data from a table in Laravel, but they have some important differences.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is TRUNCATE ?<\/h2>\n\n\n\n<p>TRUNCATE is a DDL (Data Definition Language) statement that removes all rows from a table. It is a much faster operation than DELETE because it does not log each individual row deletion. However, TRUNCATE cannot be rolled back, which means that the data is lost forever if there is an error.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is <strong>DELETE<\/strong> ?<\/h2>\n\n\n\n<p>DELETE is a DML (Data Manipulation Language) statement that removes rows from a table based on a WHERE clause. It is a slower operation than TRUNCATE because it logs each individual row deletion. However, DELETE can be rolled back, which means that the data can be recovered if there is an error.<\/p>\n\n\n\n<p><strong>Here is a table summarizing the key differences between TRUNCATE and DELETE:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>Feature<\/th><th>TRUNCATE<\/th><th>DELETE<\/th><\/tr><tr><td>Speed<\/td><td>Faster<\/td><td>Slower<\/td><\/tr><tr><td>Logging<\/td><td>Does not log<\/td><td>Logs each row deletion<\/td><\/tr><tr><td>Rollback<\/td><td>Cannot be rolled back<\/td><td>Can be rolled back<\/td><\/tr><tr><td>WHERE clause<\/td><td>Cannot use a WHERE clause<\/td><td>Can use a WHERE clause<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>In general, you should use TRUNCATE when you want to remove all rows from a table quickly and you do not need to be able to roll back the operation. You should use DELETE when you need to remove specific rows from a table or when you need to be able to roll back the operation.<\/p>\n\n\n\n<p>Here are some examples of how to use TRUNCATE and DELETE in Laravel:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DB::table('users')-&gt;truncate();<\/code><\/pre>\n\n\n\n<p>Thanks for learning \ud83d\udc4d\ud83d\udc4d<\/p>\n","protected":false},"excerpt":{"rendered":"<p>TRUNCATE and DELETE are both used to remove data from a table in Laravel, but they have some important differences. What is TRUNCATE ? TRUNCATE is a&#8230; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[589,3,588],"class_list":["post-1493","post","type-post","status-publish","format-standard","hentry","category-laravel","tag-database-delete","tag-laravel","tag-mysql-truncate"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>What is the difference between TRUNCATE and DELETE ? - DevOps Freelancer<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.devopsfreelancer.com\/blog\/what-is-the-difference-between-truncate-and-delete\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What is the difference between TRUNCATE and DELETE ? - DevOps Freelancer\" \/>\n<meta property=\"og:description\" content=\"TRUNCATE and DELETE are both used to remove data from a table in Laravel, but they have some important differences. What is TRUNCATE ? TRUNCATE is a...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.devopsfreelancer.com\/blog\/what-is-the-difference-between-truncate-and-delete\/\" \/>\n<meta property=\"og:site_name\" content=\"DevOps Freelancer\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/amitsthakurs\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-12T10:24:25+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-30T10:28:59+00:00\" \/>\n<meta name=\"author\" content=\"Amit Kumar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/amits_thakurs\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Amit Kumar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.devopsfreelancer.com\/blog\/what-is-the-difference-between-truncate-and-delete\/\",\"url\":\"https:\/\/www.devopsfreelancer.com\/blog\/what-is-the-difference-between-truncate-and-delete\/\",\"name\":\"What is the difference between TRUNCATE and DELETE ? - DevOps Freelancer\",\"isPartOf\":{\"@id\":\"https:\/\/www.devopsfreelancer.com\/blog\/#website\"},\"datePublished\":\"2023-11-12T10:24:25+00:00\",\"dateModified\":\"2023-11-30T10:28:59+00:00\",\"author\":{\"@id\":\"https:\/\/www.devopsfreelancer.com\/blog\/#\/schema\/person\/22ed4bd82dc04200a2ca541b3e35fc5b\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.devopsfreelancer.com\/blog\/what-is-the-difference-between-truncate-and-delete\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.devopsfreelancer.com\/blog\/what-is-the-difference-between-truncate-and-delete\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.devopsfreelancer.com\/blog\/what-is-the-difference-between-truncate-and-delete\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.devopsfreelancer.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"What is the difference between TRUNCATE and DELETE ?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.devopsfreelancer.com\/blog\/#website\",\"url\":\"https:\/\/www.devopsfreelancer.com\/blog\/\",\"name\":\"DevOps Freelancer\",\"description\":\"We provide DevOps | SRE | DevSecOps | MLOps Freelancing\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.devopsfreelancer.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.devopsfreelancer.com\/blog\/#\/schema\/person\/22ed4bd82dc04200a2ca541b3e35fc5b\",\"name\":\"Amit Kumar\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.devopsfreelancer.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g\",\"caption\":\"Amit Kumar\"},\"description\":\"Hi I am Amit Kumar Thakur Experienced as s Software Developer with a demonstrated history of working in the information technology and services industry. Skilled in HTML, CSS, Bootstrap4, PHP, Laravel-9 , REST API,FB API,Google API, Youtube Api, Bitbucket,Github,Linux and jQuery. Strong engineering professional focused in Computer\/Information Technology Administration and Management. Currently my profile is to Software Developer, analyze the requirement, creating frame for web application, coding and maintenance.\",\"sameAs\":[\"https:\/\/www.facebook.com\/amitsthakurs\/\",\"https:\/\/www.instagram.com\/amits_thakurs\/\",\"https:\/\/www.linkedin.com\/in\/amits-thakurs\/\",\"https:\/\/x.com\/https:\/\/twitter.com\/amits_thakurs\"],\"url\":\"https:\/\/www.devopsfreelancer.com\/blog\/author\/amit\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"What is the difference between TRUNCATE and DELETE ? - DevOps Freelancer","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.devopsfreelancer.com\/blog\/what-is-the-difference-between-truncate-and-delete\/","og_locale":"en_US","og_type":"article","og_title":"What is the difference between TRUNCATE and DELETE ? - DevOps Freelancer","og_description":"TRUNCATE and DELETE are both used to remove data from a table in Laravel, but they have some important differences. What is TRUNCATE ? TRUNCATE is a...","og_url":"https:\/\/www.devopsfreelancer.com\/blog\/what-is-the-difference-between-truncate-and-delete\/","og_site_name":"DevOps Freelancer","article_author":"https:\/\/www.facebook.com\/amitsthakurs\/","article_published_time":"2023-11-12T10:24:25+00:00","article_modified_time":"2023-11-30T10:28:59+00:00","author":"Amit Kumar","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/amits_thakurs","twitter_misc":{"Written by":"Amit Kumar","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.devopsfreelancer.com\/blog\/what-is-the-difference-between-truncate-and-delete\/","url":"https:\/\/www.devopsfreelancer.com\/blog\/what-is-the-difference-between-truncate-and-delete\/","name":"What is the difference between TRUNCATE and DELETE ? - DevOps Freelancer","isPartOf":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/#website"},"datePublished":"2023-11-12T10:24:25+00:00","dateModified":"2023-11-30T10:28:59+00:00","author":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/#\/schema\/person\/22ed4bd82dc04200a2ca541b3e35fc5b"},"breadcrumb":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/what-is-the-difference-between-truncate-and-delete\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.devopsfreelancer.com\/blog\/what-is-the-difference-between-truncate-and-delete\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.devopsfreelancer.com\/blog\/what-is-the-difference-between-truncate-and-delete\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.devopsfreelancer.com\/blog\/"},{"@type":"ListItem","position":2,"name":"What is the difference between TRUNCATE and DELETE ?"}]},{"@type":"WebSite","@id":"https:\/\/www.devopsfreelancer.com\/blog\/#website","url":"https:\/\/www.devopsfreelancer.com\/blog\/","name":"DevOps Freelancer","description":"We provide DevOps | SRE | DevSecOps | MLOps Freelancing","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.devopsfreelancer.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.devopsfreelancer.com\/blog\/#\/schema\/person\/22ed4bd82dc04200a2ca541b3e35fc5b","name":"Amit Kumar","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.devopsfreelancer.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g","caption":"Amit Kumar"},"description":"Hi I am Amit Kumar Thakur Experienced as s Software Developer with a demonstrated history of working in the information technology and services industry. Skilled in HTML, CSS, Bootstrap4, PHP, Laravel-9 , REST API,FB API,Google API, Youtube Api, Bitbucket,Github,Linux and jQuery. Strong engineering professional focused in Computer\/Information Technology Administration and Management. Currently my profile is to Software Developer, analyze the requirement, creating frame for web application, coding and maintenance.","sameAs":["https:\/\/www.facebook.com\/amitsthakurs\/","https:\/\/www.instagram.com\/amits_thakurs\/","https:\/\/www.linkedin.com\/in\/amits-thakurs\/","https:\/\/x.com\/https:\/\/twitter.com\/amits_thakurs"],"url":"https:\/\/www.devopsfreelancer.com\/blog\/author\/amit\/"}]}},"_links":{"self":[{"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/posts\/1493","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/comments?post=1493"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/posts\/1493\/revisions"}],"predecessor-version":[{"id":1494,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/posts\/1493\/revisions\/1494"}],"wp:attachment":[{"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/media?parent=1493"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/categories?post=1493"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/tags?post=1493"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}