Thursday, October 23, 2025
HomeLanguagesJavascriptp5.js clearStorage() Function

p5.js clearStorage() Function

The clearStorage() function in p5.js is used to clear all items present in the local storage set using the storeItem() function. The items are removed for the current domain. Trying to retrieve any item using the getItem() function would now return null. It accepts no parameters.

Syntax:

clearStorage()

Parameters: This function accepts no parameters.

The program below illustrates the clearStorage() function in p5.js:

Example:




function setup() {
  createCanvas(400, 300);
  textSize(16);
  text("Use the button to set and"+
       " retrieve random values", 20, 20);
  
  setBtn = createButton("Set items to storage");
  setBtn.position(20, 150);
  setBtn.mouseClicked(setItems);
  
  getBtn = createButton("Get items from storage");
  getBtn.position(20, 180);
  getBtn.mouseClicked(getItems);
  
  clearBtn = createButton("Clear items from storage");
  clearBtn.position(20, 210);
  clearBtn.mouseClicked(clearItems);
}
  
function clearItems() {
  clear();
  text("Use the button to set and retrieve"+
       " random values", 20, 20);
  text("Storage Cleared!", 20, 40);
  
  // Clear all items in storage
  clearStorage();
}
  
function getItems() {
  clear();
  text("Use the button to set and retrieve"+ 
       " random values", 20, 20);
  
  // Retrieve values from local storage
  id = getItem("savedNumber");
  author = getItem("savedString");
  isBestseller = getItem("savedBoolean");
  
  // Display the values
  text("The retrieved items are:", 20, 40);
  text("Book ID: " + id, 20, 60);
  text("Author: " + author, 20, 80);
  text("Bestseller: " + isBestseller, 20, 100);
}
  
function setItems() {
  clear();
  text("Use the button to set and retrieve"+
       " random values", 20, 20);
  text("Random items set!", 20, 40);
  
  // Generate random values
  randomID = floor(random(100));
  randomAuthor = "Author " + randomID;
  randomBool = randomID > 50 ? true : false;
  
  // Store values to local storage
  storeItem("savedNumber", randomID);
  storeItem("savedString", randomAuthor);
  storeItem("savedBoolean", randomBool);
}


Output:

clearStorage-btns

Reference: https://p5js.org/reference/#/p5/clearStorage

Online editor: https://editor.p5js.org/

Environment Setup: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/

Reference: https://p5js.org/reference/#/p5/rectMode

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS